package com.ssm.controller;

import com.ssm.core.bean.GenericResponse;
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.SsmStaffLoginService;
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 java.util.ArrayList;
import java.util.List;

@Api(value = "门店员工登录controller",tags = {"门店员工登录相关接口"})
@RequestMapping("/demay/ssm/staffLogin")
@RestController
@Slf4j
public class SsmStaffLoginController {

    @Autowired
    public SsmStaffLoginService ssmStaffLoginService;




    @ApiOperation(value = "门店员工手机验证码登陆获取token接口", notes = "获取登录接口生成的token 使用接口前必须获取一次。作为参数传入", response = GenericResponse.class)
    @RequestMapping(path="/phoneLogin", method= RequestMethod.POST)
    public GenericResponse<StaffResponse> phoneLogin(@RequestBody StaffRequest staffRequest) {
        GenericResponse<StaffResponse> genericResponse = new GenericResponse<>();
        StaffResponse staffResponse = null;
        if (StringUtils.isBlank(staffRequest.getMobilePhone())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"手机号");
            return genericResponse;
        }
        if (StringUtils.isBlank(staffRequest.getPassCode())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"验证码");
            return genericResponse;
        }
        try{
            staffResponse = ssmStaffLoginService.uniPhoneLogin(staffRequest);
        }catch (SmsException e){
            log.error("SsmStaffLoginController.phoneLogin:", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmStaffLoginController.phoneLogin:", e);
            genericResponse.setCodeAndMsg(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
            return genericResponse;
        }
        if (staffResponse == null){
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setData(staffResponse);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "门店员工获取手机短信验证码接口", notes = "用户获取手机短信验证码接口", response = GenericResponse.class)
    @RequestMapping(path="/getSmsCode", method= RequestMethod.POST)
    public GenericResponse<StaffResponse> getSmsCode(@RequestBody StaffRequest staffRequest) {
        GenericResponse<StaffResponse> genericResponse = new GenericResponse<>();
        StaffResponse staffResponse = null;
        if (StringUtils.isBlank(staffRequest.getMobilePhone())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"手机号");
            return genericResponse;
        }
        try{
            staffResponse = ssmStaffLoginService.getSmsCode(staffRequest);
        }catch (SmsException e){
            log.error("SsmStaffLoginController.getSmsCode:", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmStaffLoginController.getSmsCode:", e);
            genericResponse.setCodeAndMsg(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
            return genericResponse;
        }
        if (staffResponse == null){
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setData(staffResponse);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }


    @ApiOperation(value = "门店员工修改密码接口", notes = "用户修改密码接口", response = String.class)
    @RequestMapping(path="/changePassword", method= RequestMethod.POST)
    public GenericResponse<StaffResponse> changePassword(@RequestBody StaffRequest staffRequest) {
        GenericResponse<StaffResponse> genericResponse = new GenericResponse<>();

        int i = 0;
        try{
            i = ssmStaffLoginService.changePassword(staffRequest);
        }catch (SmsException e){
            log.error("SsmStaffLoginController.changePassword:", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmStaffLoginController.changePassword:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }


    @ApiOperation(value = "门店员工手机密码登陆获取token接口", notes = "获取登录接口生成的token 使用接口前必须获取一次。作为参数传入", response = GenericResponse.class)
    @RequestMapping(path="/phonePassLogin", method= RequestMethod.POST)
    public GenericResponse<StaffResponse> phonePassLogin(@RequestBody StaffRequest staffRequest) {
        GenericResponse<StaffResponse> genericResponse = new GenericResponse<>();
        StaffResponse staffResponse = null;
        if (StringUtils.isBlank(staffRequest.getMobilePhone())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"手机号");
            return genericResponse;
        }
        if (StringUtils.isBlank(staffRequest.getPassword())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"密码");
            return genericResponse;
        }
        try{
            staffResponse = ssmStaffLoginService.uniPhonePassLogin(staffRequest);
        }catch (SmsException e){
            log.error("SsmStaffLoginController.phonePassLogin:", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmStaffLoginController.phonePassLogin:", e);
            genericResponse.setCodeAndMsg(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
            return genericResponse;
        }
        if (staffResponse == null){
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setData(staffResponse);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "退出登录",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/logout")
    public GenericResponse logout (@RequestBody StaffRequest staffRequest){
        GenericResponse genericResponse = new GenericResponse();
        if (StringUtils.isBlank(staffRequest.getMobilePhone())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "手机号");
            return genericResponse;
        }
        try {
            ssmStaffLoginService.uniLogout(staffRequest);
        }catch (Exception e){
            log.error("SsmStaffLoginController.logout",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }


    @ApiOperation(value = "通过手机号查询员工所属门店",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/list")
    public GenericResponse list (@RequestBody StaffRequest staffRequest){
        GenericResponse genericResponse = new GenericResponse();
        List<BaseStaff> list = new ArrayList<>();
        try {
            list = ssmStaffLoginService.list(staffRequest);
        }catch (Exception e){
            log.error("SsmStaffLoginController.list",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

}
