package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.core.bean.GenericResponse;
import com.ssm.core.bean.PageResponse;
import com.ssm.core.util.SessionHelp;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.entity.StaticBaseStaff;
import com.ssm.manage.entity.SysStoreRole;
import com.ssm.manage.query.SysStoreRoleRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.StaffResponse;
import com.ssm.manage.service.SsmSysStoreRoleService;
import com.ssm.util.RedisClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.filters.ExpiresFilter;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
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.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Api(value="门店角色（新登录）controller",tags={"门店角色（新登录）相关接口"})
@RestController
@Slf4j
@RequestMapping("/demay/ssm/SysStoreRole")
public class SsmSysStoreRoleContorller {

    @Autowired
    private SsmSysStoreRoleService ssmSysStoreRoleService;
    @Autowired
    public RedisClient redisClient;
    @ApiOperation(value = "分页获取角色信息  门店端-角色-分页获取角色信息", httpMethod = "POST", response = PageResponse.class, notes = "分页获取菜单信息")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public PageResponse<SysStoreRole> selectByPage(@RequestBody SysStoreRoleRequest sysStoreRole, HttpSession session) {
        PageResponse<SysStoreRole> pageResponse = new PageResponse<>();
        PageInfo<SysStoreRole> pageInfo;
        try {
            sysStoreRole.setStoreId((long)redisClient.get("StoreId"));
            //sysStoreRole.setStoreId(SessionHelp.getStoreId(session));
            pageInfo = ssmSysStoreRoleService.selectPage(sysStoreRole);
        } catch (Exception e) {
            log.error("SsmSysRoleController.selectPage:", 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 = "/add", method = RequestMethod.POST)
    public GenericResponse add(@RequestBody SysStoreRoleRequest sysStoreRole, HttpSession session, HttpServletRequest request) {
        GenericResponse genericResponse = new GenericResponse();
        if (StringUtils.isBlank(sysStoreRole.getRoleName())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "角色姓名");
            return genericResponse;
        }

        if (StringUtils.isBlank(sysStoreRole.getRoleNum())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "角色编码");
            return genericResponse;
        }
        StaticBaseStaff.getBaseStaff().getStoreId();
        //String userNum = SessionHelp.getLoginAccount(session);
        //sysStoreRole.setStoreId(SessionHelp.getStoreId(session));
        String userNum = (String)redisClient.get("Account");
        sysStoreRole.setStoreId((long)redisClient.get("StoreId"));

        int i = 0;
        try {
            i = ssmSysStoreRoleService.saveSysStoreRole(sysStoreRole,userNum);
        }catch (SmsException e) {
            log.error("SsmSysRoleController.add:", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e) {
            log.error("SsmSysRoleController.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 SysStoreRoleRequest sysStoreRole, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        if (StringUtils.isBlank(sysStoreRole.getRoleName())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "角色姓名");
            return genericResponse;
        }
        if (StringUtils.isBlank(sysStoreRole.getRoleNum())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "角色编码");
            return genericResponse;
        }
        if (sysStoreRole.getId() == null){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"id");
        }
        String userNum = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmSysStoreRoleService.updateSysStoreRole(sysStoreRole,userNum);
        }catch (SmsException e) {
            log.error("SsmSysRoleController.add:", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e) {
            log.error("SsmSysRoleController.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 = "/updateState", method = RequestMethod.POST)
    public GenericResponse updateState(@RequestBody SysStoreRoleRequest sysStoreRole, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        if (sysStoreRole.getId() == null){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"id");
        }
        int i = 0;
        try {
            i = ssmSysStoreRoleService.updateSysRoleState(sysStoreRole);
        }catch (Exception e) {
            log.error("SsmSysRoleController.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 = GenericResponse.class, notes = "")
    @RequestMapping(value = "/addRoleStore", method = RequestMethod.POST)
    public GenericResponse addRoleStore(@RequestBody SysStoreRoleRequest sysStoreRole, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        int i = 0;
        try {
            i = ssmSysStoreRoleService.addRoleStore(sysStoreRole);
        }catch (SmsException e) {
            log.error("SsmSysRoleController.add:", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e) {
            log.error("SsmSysRoleController.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 = GenericResponse.class, notes = "")
    @RequestMapping(value = "/addMuenRole", method = RequestMethod.POST)
    public GenericResponse addMuenRole(@RequestBody SysStoreRoleRequest sysStoreRole, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        int i = 0;
        try {
            i = ssmSysStoreRoleService.addMuenRole(sysStoreRole);
        }catch (SmsException e) {
            log.error("SsmSysRoleController.add:", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e) {
            log.error("SsmSysRoleController.updateState:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }
}
