package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.response.PageResponse;
import com.ssm.manage.utils.SessionHelp;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.entity.SysRoleUser;
import com.ssm.manage.entity.SysUser;
import com.ssm.manage.query.RoleUserRequest;
import com.ssm.manage.query.UserRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmSysRoleUserService;
import com.ssm.manage.service.SsmSysUserService;
import com.ssm.manage.utils.CommonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@Api(value="平台用户controller",tags={"平台用户相关接口"})
@RestController
@RequestMapping("/demay/ssm/user")
@Slf4j
public class SsmSysUserController extends SsmSysBaseController{

    @Autowired
    public SsmSysUserService ssmSysUserService;
    @Autowired
    public SsmSysRoleUserService ssmSysRoleUserService;

    @ApiOperation(value = "分页获取用户信息  平台端-用户-分页获取用户信息", httpMethod = "POST", response = PageResponse.class, notes = "分页获取用户信息")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public PageResponse<SysUser> selectByPage(@RequestBody UserRequest userRequest) {
        PageResponse<SysUser> pageResponse = new PageResponse<>();
        PageInfo<SysUser> pageInfo;
        try {
            pageInfo = ssmSysUserService.selectByPage(userRequest);
        } catch (Exception e) {
            log.error("SsmSysUserController.selectByPage:", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    @ApiOperation(value = "批量删除用户  平台端-用户-批量删除用户", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public GenericResponse delete(@RequestBody long[] ids) {
        GenericResponse genericResponse = new GenericResponse();
        if (null == ids || ids.length < 1) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "ids");
            return genericResponse;
        }

        UserRequest userRequest = new UserRequest();
        userRequest.setIds(ids);
        int i = 0;
        try {
            i = ssmSysUserService.deleteSysUser(userRequest);
        } catch (Exception e){
            log.error("SsmSysUserController.delete:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }


    @ApiOperation(value = "单个删除用户   平台端-用户-单个删除用户", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/deleteSingle", method = RequestMethod.POST)
    public GenericResponse deleteSigle(@RequestBody UserRequest userRequest) {
        GenericResponse genericResponse = new GenericResponse();
        if (userRequest.getId() == null){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"id");
        }
        int i = 0;
        try {
            i = ssmSysUserService.deleteSingleSysUser(userRequest);
        } catch (Exception e){
            log.error("SsmSysUserController.deleteSingle:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "添加用户  平台端-用户-添加用户", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public GenericResponse add(@RequestBody SysUser sysUser, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        if (StringUtils.isBlank(sysUser.getUserNum())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "用户账户");
            return genericResponse;
        }
        if (StringUtils.isBlank(sysUser.getMobilePhone()) || !CommonUtils.isMobile(sysUser.getMobilePhone())) {
            genericResponse.setCodeAndMsg(ResponseCode.NOT_MEET_REQUIREMENTS_S, "电话");
            return genericResponse;
        }

//        sysUser.setMobilePhone(CommonUtils.mobileFormat(sysUser.getMobilePhone()));
        String userNum = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmSysUserService.saveSysUser(sysUser,userNum);
        } catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmSysUserController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }


    @ApiOperation(value = "修改用户   平台端-用户-修改用户", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public GenericResponse update(@RequestBody SysUser sysUser, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        if (StringUtils.isNotBlank(sysUser.getMobilePhone()) && !CommonUtils.isMobile(sysUser.getMobilePhone())) {
            genericResponse.setCodeAndMsg(ResponseCode.NOT_MEET_REQUIREMENTS_S, "电话");
            return genericResponse;
        }
        if (sysUser.getId() == null){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"id");
            return genericResponse;
        }
//        sysUser.setMobilePhone(CommonUtils.mobileFormat(sysUser.getMobilePhone()));
        String userNum = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmSysUserService.updateSysUser(sysUser,userNum);
        } catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e) {
            log.error("SsmSysUserController.update:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "修改用户状态    平台端-用户-修改用户状态", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/updateState", method = RequestMethod.POST)
    public GenericResponse updateState(@RequestBody UserRequest userRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();

        String userNum = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmSysUserService.updateSysUserState(userRequest,userNum);
        } catch (Exception e) {
            log.error("SsmSysUserController.updateState:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }


    @ApiOperation(value = "分页获取当前用户关联的角色信息    平台端-用户-分页获取当前用户关联的角色信息", httpMethod = "POST", response = PageResponse.class, notes = "分页获取当前用户关联的角色信息")
    @RequestMapping(value = "/rolePage", method = RequestMethod.POST)
    public PageResponse<SysRoleUser> rolePage(@RequestBody RoleUserRequest roleUserRequest) {
        PageResponse<SysRoleUser> pageResponse = new PageResponse<>();
        PageInfo<SysRoleUser> pageInfo;
        try {
            pageInfo = ssmSysRoleUserService.rolePage(roleUserRequest);
        } catch (Exception e) {
            log.error("SsmSysUserController.rolePage:", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    @ApiOperation(value = "用户角色分配   平台端-用户-用户角色分配", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/addUserRole", method = RequestMethod.POST)
    public GenericResponse addUserRole(@RequestBody RoleUserRequest roleUserRequest) {
        GenericResponse genericResponse = new GenericResponse();


        int i = 0;
        try {
            i = ssmSysRoleUserService.addRoleUser(roleUserRequest);
        } catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmSysUserController.addUserRole:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }
}
