package com.ssm.controller;

import com.ssm.core.bean.GenericResponse;
import com.ssm.core.util.SessionHelp;
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 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.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
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;
    @Autowired
    public RedisClient redisClient;




    @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();

        try {
            ssmStaffLoginService.uniLogout(staffRequest);
            redisClient.delete("StoreId");
            redisClient.delete("Account");
        }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;
    }
    @ApiOperation(value = "门店员工手机密码登陆获取token接口(新的登录)", notes = "获取登录接口生成的token 使用接口前必须获取一次。作为参数传入", response = GenericResponse.class)
    @RequestMapping(path="/phonePassLoginNew", method= RequestMethod.POST)
    public GenericResponse<StaffResponse> phonePassLoginNew(@RequestBody StaffRequest staffRequest, HttpSession session) {
        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.unphonePassLoginNew(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);
        if(staffResponse.getStoreType()==2){
            SessionHelp.setToken(session,staffResponse.getToken());
            SessionHelp.setLoginAccount(session,staffRequest.getMobilePhone());
            SessionHelp.setStoreId(session,staffResponse.getStoreId());
            SessionHelp.setStoreName(session,staffResponse.getStoreName());
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "门店员工手机密码登陆获取token接口（总店登录，选择完门店）(新的登录)", notes = "获取登录接口生成的token 使用接口前必须获取一次。作为参数传入", response = GenericResponse.class)
    @RequestMapping(path="/phonePassLoginNews", method= RequestMethod.POST)
    public GenericResponse<StaffResponse> phonePassLoginNews(@RequestBody StaffRequest staffRequest, HttpSession session) {
        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.unphonePassLoginNews(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);
        if(staffResponse.getStoreType()==2){
            redisClient.set("StoreId",staffResponse.getStoreId());
            redisClient.set("Account",staffRequest.getMobilePhone());

            //redisClient.set(staffResponse.getToken(),staffResponse.getStoreId());
            //redisClient.set(staffResponse.getToken(),staffRequest.getMobilePhone());
            //Long aa=staffResponse.getId();
            //System.out.println(aa);
            //redisClient.set(String.valueOf((long)staffResponse.getId()),staffResponse.getStoreId());
            //redisClient.set(String.valueOf((long)staffResponse.getId()),staffRequest.getMobilePhone());

            SessionHelp.setToken(session,staffResponse.getToken());
            SessionHelp.setLoginAccount(session,staffRequest.getMobilePhone());
            SessionHelp.setStoreId(session,staffResponse.getStoreId());
            SessionHelp.setStoreName(session,staffResponse.getStoreName());
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }


    @ApiOperation(value = "门店员工手机密码登陆获取token接口（总店登录，选择完门店）(新的登录)", notes = "获取登录接口生成的token 使用接口前必须获取一次。作为参数传入", response = GenericResponse.class)
    @RequestMapping(path="/phonePassLogindzz", method= RequestMethod.POST)
    public GenericResponse<StaffResponse> phonePassLogindzz(@RequestBody StaffRequest staffRequest, HttpSession session) {
        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.unphonePassLogindzz(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;
        }
        if(staffResponse.getStoreType()==2){
            SessionHelp.setToken(session,staffResponse.getToken());
            SessionHelp.setLoginAccount(session,staffRequest.getMobilePhone());
            SessionHelp.setStoreId(session,staffResponse.getStoreId());
            SessionHelp.setStoreName(session,staffResponse.getStoreName());
        }
        genericResponse.setData(staffResponse);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "门店员工手机验证码登陆获取token接口(新登录)", notes = "获取登录接口生成的token 使用接口前必须获取一次。作为参数传入", response = GenericResponse.class)
    @RequestMapping(path="/phoneLoginNew", method= RequestMethod.POST)
    public GenericResponse<StaffResponse> phoneLoginNew(@RequestBody StaffRequest staffRequest, HttpSession session) {
        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.phoneLoginNew(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);
        if(staffResponse.getStoreType()==2){
            SessionHelp.setToken(session,staffResponse.getToken());
            SessionHelp.setLoginAccount(session,staffRequest.getMobilePhone());
            SessionHelp.setStoreId(session,staffResponse.getStoreId());
            SessionHelp.setStoreName(session,staffResponse.getStoreName());
        }

//        StoreRequest storeRequest = new StoreRequest();
//        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
//        List<Long> objects = new ArrayList<>();
//        for (BaseStores pagesorganization : pagesorganizations) {
//            objects.add(pagesorganization.getId());
//        }
//        SessionHelp.setStoreIds(objects);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "门店员工手机验证码登陆获取token接口,多组织选择组织(新登录)", notes = "获取登录接口生成的token 使用接口前必须获取一次。作为参数传入", response = GenericResponse.class)
    @RequestMapping(path="/phoneLoginNewzz", method= RequestMethod.POST)
    public GenericResponse<StaffResponse> phoneLoginNewzz(@RequestBody StaffRequest staffRequest, HttpSession session) {
        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.phoneLoginNewzz(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);
        SessionHelp.setToken(session,staffResponse.getToken());
        SessionHelp.setLoginAccount(session,staffRequest.getMobilePhone());
        SessionHelp.setStoreId(session,staffResponse.getStoreId());
        SessionHelp.setStoreName(session,staffRequest.getStoreName());

//        StoreRequest storeRequest = new StoreRequest();
//        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
//        List<Long> objects = new ArrayList<>();
//        for (BaseStores pagesorganization : pagesorganizations) {
//            objects.add(pagesorganization.getId());
//        }
//        SessionHelp.setStoreIds(objects);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "门店员工手机验证码登陆获取token接口,多组织选择组织(新登录)", notes = "获取登录接口生成的token 使用接口前必须获取一次。作为参数传入", response = GenericResponse.class)
    @RequestMapping(path="/phoneLoginNews", method= RequestMethod.POST)
    public GenericResponse<StaffResponse> phoneLoginNews(@RequestBody StaffRequest staffRequest, HttpSession session) {
        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.phoneLoginNews(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);
        SessionHelp.setToken(session,staffResponse.getToken());
        SessionHelp.setLoginAccount(session,staffRequest.getMobilePhone());
        SessionHelp.setStoreId(session,staffResponse.getStoreId());
        SessionHelp.setStoreName(session,staffRequest.getStoreName());

//        StoreRequest storeRequest = new StoreRequest();
//        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
//        List<Long> objects = new ArrayList<>();
//        for (BaseStores pagesorganization : pagesorganizations) {
//            objects.add(pagesorganization.getId());
//        }
//        SessionHelp.setStoreIds(objects);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }
}
