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.utils.AESUtils;
import com.ssm.manage.entity.BaseStaff;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.StaffRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.StaffResponse;
import com.ssm.manage.service.SsmBaseStaffService;
import com.ssm.manage.service.SsmBaseStoreService;
import com.ssm.manage.utils.CommonUtils;
import com.ssm.util.RedisClient;
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.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Api(value="员工controller",tags={"员工相关接口"})
@RestController
@RequestMapping("/demay/ssm/staff")
@Slf4j
public class SsmStaffController {
    @Autowired
    private SsmBaseStaffService ssmBaseStaffService;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

    @Autowired
    private RedisClient<StaffResponse> redisClient;

    @ApiOperation(value = "分页查询员工信息  美发版-员工-分页查询员工信息",httpMethod = "POST",notes = "",response = PageResponse.class)
    @RequestMapping("/page")
    public PageResponse selectPage(@RequestBody StaffRequest staffRequest){
        PageResponse<StaffResponse> pageResponse = new PageResponse<>();
        PageInfo<StaffResponse> pageInfo = new PageInfo<>();
        try {
            pageInfo = ssmBaseStaffService.selectPage(staffRequest);
        }catch (Exception e){
            log.error("SsmStaffController.selectPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setPageInfo(pageInfo);
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        return pageResponse;
    }

    @ApiOperation(value = "查询员工明细  美发版-员工-查询员工明细", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/selectOne")
    public GenericResponse selectOne(@RequestBody StaffRequest staffRequest) {
        GenericResponse<BaseStaff> genericResponse = new GenericResponse();
        BaseStaff baseStaff = new BaseStaff();
        try {
            baseStaff = ssmBaseStaffService.selectOne(staffRequest);
        } catch (Exception e) {
            log.error("SsmStaffController.selectOne:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setData(baseStaff);
        return genericResponse;
    }

    @ApiOperation(value = "添加员工  美发版-员工-添加员工", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/add")
    public GenericResponse add(@RequestBody StaffRequest staffRequest, HttpSession session) {
        GenericResponse<BaseStaff> genericResponse = new GenericResponse();
        if (StringUtils.isBlank(staffRequest.getStaffName())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "员工名称");
            return genericResponse;
        }
        if (StringUtils.isBlank(staffRequest.getMobilePhone())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "手机号码");
            return genericResponse;
        }
        if (!CommonUtils.isMobile(staffRequest.getMobilePhone())) {
            genericResponse.setCodeAndMsg(ResponseCode.NOT_MEET_REQUIREMENTS_S, "手机号码");
            return genericResponse;
        }
        String password = staffRequest.getPassword();
        staffRequest.setPassword(AESUtils.encrypt(password));
        int i = 0;
        try {
            i = ssmBaseStaffService.addStaff(staffRequest,null);
        } catch (SmsException e){
            log.error("SsmStaffController.add:",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStaffController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
//            BaseStore baseStore = ssmBaseStoreService.selectByPrimaryKey(staffRequest.getStoreId());
//            //设置短信账户 发送 否则不发送
//            if (StringUtils.isNotBlank(baseStore.getSmsAccount())) {
//                try {
//                    ChuangLanSmsUtil.sendStaffInitPwdSms(baseStore, staffRequest.getMobilePhone(), Integer.parseInt(password));
//                } catch (IOException e) {
//                    log.error("SsmStaffController.add:", e);
//                } catch (SmsException e) {
//                    log.error("SsmStaffController.add:", e);
//                }
//            }
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "员工修改   美发版-员工-员工修改", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/update")
    public GenericResponse update(@RequestBody StaffRequest staffRequest) {
        GenericResponse<BaseStaff> genericResponse = new GenericResponse();
//        if (StringUtils.isBlank(staffRequest.getMobilePhone())) {
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "手机号码");
//            return genericResponse;
//        }
//        if (!CommonUtils.isMobile(staffRequest.getMobilePhone())) {
//            genericResponse.setCodeAndMsg(ResponseCode.NOT_MEET_REQUIREMENTS_S, "手机号码");
//            return genericResponse;
//        }
        int i = 0;
        try {
            i = ssmBaseStaffService.updateStaff(staffRequest,null);
        } catch (SmsException e){
            log.error("SsmStaffController.update:",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStaffController.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("/monthJob")
    public GenericResponse monthJob(@RequestBody StaffRequest staffRequest) {
        GenericResponse<BaseStaff> genericResponse = new GenericResponse();
        int i = 0;
        try {
            i = ssmBaseStaffService.setMonthjob(staffRequest);
        } catch (Exception e) {
            log.error("SsmStaffController.monthJob:", 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("/leave")
    public GenericResponse leave(@RequestBody StaffRequest staffRequest) {
        GenericResponse<BaseStaff> genericResponse = new GenericResponse();
        int i = 0;
        try {
            i = ssmBaseStaffService.staffLeave(staffRequest);
        } catch (Exception e) {
            log.error("SsmStaffController.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("/reinstated")
    public GenericResponse reinstated(@RequestBody StaffRequest staffRequest) {
        GenericResponse<BaseStaff> genericResponse = new GenericResponse();
        if (StringUtils.isBlank(staffRequest.getMobilePhone())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "手机号码");
            return genericResponse;
        }
        int i = 0;
        try {
            i = ssmBaseStaffService.staffReinstated(staffRequest);
        } catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStaffController.reinstated:", 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)
    @RequestMapping("/selectList")
    public GenericResponse selectList(@RequestBody StaffRequest staffRequest) {
        GenericResponse<BaseStaff> genericResponse = new GenericResponse();
        List<BaseStaff> list = new ArrayList<>();
        try {
            list = ssmBaseStaffService.selectList(staffRequest);
        } catch (Exception e) {
            log.error("SsmStaffController.selectList:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

    @ApiOperation(value = "门店员工启用  门店端-员工列表-门店员工启用", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/start")
    public GenericResponse start(@RequestBody StaffRequest staffRequest) {
        GenericResponse<BaseStaff> genericResponse = new GenericResponse();
        if (StringUtils.isBlank(staffRequest.getMobilePhone())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "手机号码");
            return genericResponse;
        }
        int i = 0;
        try {
            i = ssmBaseStaffService.start(staffRequest);
        } catch (Exception e) {
            log.error("SsmStaffController.start:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

}
