package com.ssm.controller;

import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.utils.SessionHelp;
import com.ssm.manage.entity.BaseStore;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.entity.StoreParament;
import com.ssm.manage.entity.SysMuenRole;
import com.ssm.manage.mapper.SmsFixMapper;
import com.ssm.manage.query.StoreParamentquest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.StaffResponse;
import com.ssm.manage.service.SsmBaseStoreService;
import com.ssm.manage.service.SsmStoreParaService;
import com.ssm.manage.service.SsmSysMuenRoleService;
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.ObjectUtils;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

@Api(value="门店参数controller",tags={"门店参数相关接口"})
@RestController
@RequestMapping("/demay/ssm/storePara")
@Slf4j
public class SsmStoreParaController {
    @Autowired
    private SsmStoreParaService ssmStoreParaService;

    @Autowired
    private RedisClient<StaffResponse> redisClient;
    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

    @Autowired
    private SmsFixMapper smsFixMapper;

    @Autowired
    private SsmSysMuenRoleService ssmSysMuenRoleService;


    @ApiOperation(value = "门店参数列表  门店端-门店参数-门店参数列表",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/list")
        public GenericResponse list(@RequestBody StoreParamentquest storeParament, HttpSession session){
        GenericResponse<StoreParament> genericResponse = new GenericResponse();
        if(null ==storeParament.getStoreId()){
            storeParament.setStoreId(SessionHelp.getStoreId(session));
        }
        //获取登录人信息
        String phone= SessionHelp.getLoginAccount(session);
        StaffResponse staffResponse = redisClient.get(ssmStoreParaService.getKey(phone));

        List<StoreParament> list = new ArrayList<>();
        try {

            SysMuenRole role=new SysMuenRole();
            role.setRoleId(staffResponse.getRoleId());
            boolean check=ssmSysMuenRoleService.checkRole(role);
            if(storeParament.getType()!=null){
                if(check){
                    list = ssmStoreParaService.list(storeParament.getStoreId());
                }else{
                    throw new SmsException(ResponseCode.MSG,"操作权限不足");
                }
            }else{
                list = ssmStoreParaService.list(storeParament.getStoreId());
            }



        }catch (SmsException e){
            log.error("SsmStoreParaController.list",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmStoreParaController.list",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

//    @ApiOperation(value = "添加门店参数", httpMethod = "POST", response = GenericResponse.class, notes = "")
//    @RequestMapping("/add")
//    public GenericResponse add(@RequestBody StoreParaRequest storeParaRequest, HttpSession session) {
//        GenericResponse<StoreParament> genericResponse = new GenericResponse();
//
//        Long storeId = SessionHelp.getStoreId(session);
//        String storeName = SessionHelp.getStoreName(session);
//        int i = 0;
//        try {
//            i = ssmStoreParaService.addStorePara(storeParaRequest,storeId,storeName);
//        } catch (Exception e) {
//            log.error("SsmStoreParaController.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("/update")
//    public GenericResponse update(@RequestBody StoreParaRequest storeParaRequest, HttpSession session) {
//        GenericResponse<StoreParament> genericResponse = new GenericResponse();
//
//        Long storeId = SessionHelp.getStoreId(session);
//        String storeName = SessionHelp.getStoreName(session);
//        int i = 0;
//        try {
//            i = ssmStoreParaService.updateStorePara(storeParaRequest,storeId,storeName);
//        } catch (Exception e) {
//            log.error("SsmStoreParaController.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("/adds")
    public GenericResponse adds(@RequestParam Map<String,Object> fromMap, HttpSession session) {
        GenericResponse<StoreParament> genericResponse = new GenericResponse();

        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        int i = 0;
        try {
            i = ssmStoreParaService.addStoreParas(fromMap,storeId,storeName);
        } catch (Exception e) {
            log.error("SsmStoreParaController.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("/update")
    public GenericResponse update(@RequestParam Map<String,Object> formMap, HttpSession session) {
        GenericResponse<StoreParament> genericResponse = new GenericResponse();
        String storeId1 = (String) formMap.get("storeId");
        Long storeId = Long.valueOf(storeId1);
//        Long storeId = (Long) formMap.get("storeId");
        formMap.remove("storeId");
//        Long  storeId = SessionHelp.getStoreId(session);
        String  storeName = SessionHelp.getStoreName(session);

        Integer allowdebt=0;
        Integer recDebt=0;
        Integer cardCangeListDebt=0;

        String debtUsable=null;
        String debtRatio=null;

        String recDebtUsable=null;
        String recDebtRatio=null;

        String cardCangeListDebtUsable=null;
        String cardCangeListRatio=null;


        for (Map.Entry<String, Object> entry : formMap.entrySet()) {
            String paraName = entry.getKey();
            if(paraName.equals("allowdebt")){
                String paraValue = ObjectUtils.toString(entry.getValue());
                String[] valueRemark = paraValue.split("\\|");
                if(valueRemark[0].equals("1")){
                    allowdebt=Integer.valueOf(valueRemark[0]);
                }
            }
            if(paraName.equals("recDebt")){
                String paraValue = ObjectUtils.toString(entry.getValue());
                String[] valueRemark = paraValue.split("\\|");
                if(valueRemark[0].equals("1")){
                    recDebt=Integer.valueOf(valueRemark[0]);
                }
            }
            if(paraName.equals("cardCangeListDebt")){
                String paraValue = ObjectUtils.toString(entry.getValue());
                String[] valueRemark = paraValue.split("\\|");
                if(valueRemark[0].equals("1")){
                    cardCangeListDebt=Integer.valueOf(valueRemark[0]);
                }
            }


            if(paraName.equals("debtUsable")){
                String paraValue = ObjectUtils.toString(entry.getValue());
                String[] valueRemark = paraValue.split("\\|");
                    debtUsable=valueRemark[0];
            }
            if(paraName.equals("debtRatio")){
                String paraValue = ObjectUtils.toString(entry.getValue());
                String[] valueRemark = paraValue.split("\\|");
                debtRatio=valueRemark[0];
            }
            if(paraName.equals("recDebtUsable")){
                String paraValue = ObjectUtils.toString(entry.getValue());
                String[] valueRemark = paraValue.split("\\|");
                recDebtUsable=valueRemark[0];
            }
            if(paraName.equals("recDebtRatio")){
                String paraValue = ObjectUtils.toString(entry.getValue());
                String[] valueRemark = paraValue.split("\\|");
                recDebtRatio=valueRemark[0];
            }
            if(paraName.equals("cardCangeListDebtUsable")){
                String paraValue = ObjectUtils.toString(entry.getValue());
                String[] valueRemark = paraValue.split("\\|");
                cardCangeListDebtUsable=valueRemark[0];
            }
            if(paraName.equals("cardCangeListRatio")){
                String paraValue = ObjectUtils.toString(entry.getValue());
                String[] valueRemark = paraValue.split("\\|");
                cardCangeListRatio=valueRemark[0];
            }


        }

        int i = 0;
        try {
            //疗程欠款
            if(allowdebt==1){
                if(debtUsable==null||debtRatio==null){
                    throw new SmsException(ResponseCode.NONULL);
                }
            }
            //充值欠款
            if(recDebt==1){
                if(recDebtUsable==null||recDebtRatio==null){
                    throw new SmsException(ResponseCode.NONULL);
                }
            }
            //加价转疗程欠款
            if(cardCangeListDebt==1){
                if(cardCangeListDebtUsable==null||cardCangeListRatio==null){
                    throw new SmsException(ResponseCode.NONULL);
                }
            }
            i = ssmStoreParaService.updateStorePara(formMap,storeId,storeName);
        } catch (SmsException e){
            log.error("SsmStoreParaController.update:", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStoreParaController.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("/update1")
    public GenericResponse update1(@RequestParam Map<String,Object> formMap, HttpSession session,@RequestBody List<Long> storeIdList) {
        GenericResponse<StoreParament> genericResponse = new GenericResponse();
        List<StoreParament> storeIds=new ArrayList<>();
        String storeId1 = (String) formMap.get("storeId");
        Long storeId = Long.valueOf(storeId1.trim());
//        Long storeId = (Long) formMap.get("storeId");
//        formMap.remove("storeId");
//        Long  storeId = SessionHelp.getStoreId(session);
        String  storeName = SessionHelp.getStoreName(session);
        if(formMap==null){
            StoreParament storeParament=new StoreParament();
            storeParament.setStoreId(storeId);
            storeParament.setStoreName(storeName) ;
            storeIds.set(0,storeParament);
        }
        Integer allowdebt=0;
        Integer recDebt=0;
        Integer cardCangeListDebt=0;

        String debtUsable=null;
        String debtRatio=null;

        String recDebtUsable=null;
        String recDebtRatio=null;

        String cardCangeListDebtUsable=null;
        String cardCangeListRatio=null;
        int i = 0;
            if(storeIdList!=null && storeIdList.size()>0){
                for (Long aLong : storeIdList) {
                    BaseStore baseStore = ssmBaseStoreService.selectByPrimaryKey(aLong);
                    StoreParament storeParament = new StoreParament();
                    storeParament.setStoreId(baseStore.getId());
                    storeParament.setStoreName(baseStore.getStoreName());
                    storeIds.add(storeParament);
                }
            }

        for (Map.Entry<String, Object> entry : formMap.entrySet()) {
            String paraName = entry.getKey();
            if(paraName.equals("toreIds")){
                String paraValue = ObjectUtils.toString(entry.getValue());
                String[] valueRemark = paraValue.split("\\|");
                StoreParament parament = new StoreParament();
                parament.setStoreId(Long.valueOf(valueRemark[0].trim()));
                parament.setStoreName(valueRemark[1]);
                if(!storeIds.contains(parament)) {
                    storeIds.add(parament);
                }
            }
            if(paraName.equals("allowdebt")){
                String paraValue = ObjectUtils.toString(entry.getValue());
                String[] valueRemark = paraValue.split("\\|");
                if(valueRemark[0].equals("1")){
                    allowdebt=Integer.valueOf(valueRemark[0]);
                }
            }
            if(paraName.equals("recDebt")){
                String paraValue = ObjectUtils.toString(entry.getValue());
                String[] valueRemark = paraValue.split("\\|");
                if(valueRemark[0].equals("1")){
                    recDebt=Integer.valueOf(valueRemark[0]);
                }
            }
            if(paraName.equals("cardCangeListDebt")){
                String paraValue = ObjectUtils.toString(entry.getValue());
                String[] valueRemark = paraValue.split("\\|");
                if(valueRemark[0].equals("1")){
                    cardCangeListDebt=Integer.valueOf(valueRemark[0]);
                }
            }

            if(paraName.equals("debtUsable")){
                String paraValue = ObjectUtils.toString(entry.getValue());
                String[] valueRemark = paraValue.split("\\|");
                debtUsable=valueRemark[0];
            }
            if(paraName.equals("debtRatio")){
                String paraValue = ObjectUtils.toString(entry.getValue());
                String[] valueRemark = paraValue.split("\\|");
                debtRatio=valueRemark[0];
            }
            if(paraName.equals("recDebtUsable")){
                String paraValue = ObjectUtils.toString(entry.getValue());
                String[] valueRemark = paraValue.split("\\|");
                recDebtUsable=valueRemark[0];
            }
            if(paraName.equals("recDebtRatio")){
                String paraValue = ObjectUtils.toString(entry.getValue());
                String[] valueRemark = paraValue.split("\\|");
                recDebtRatio=valueRemark[0];
            }
            if(paraName.equals("cardCangeListDebtUsable")){
                String paraValue = ObjectUtils.toString(entry.getValue());
                String[] valueRemark = paraValue.split("\\|");
                cardCangeListDebtUsable=valueRemark[0];
            }
            if(paraName.equals("cardCangeListRatio")){
                String paraValue = ObjectUtils.toString(entry.getValue());
                String[] valueRemark = paraValue.split("\\|");
                cardCangeListRatio=valueRemark[0];
            }

        }

        /*if(storeIds.size()<=0){
            StoreParament storeParament=new StoreParament();
            storeParament.setStoreId(SessionHelp.getStoreId(session));
            storeParament.setStoreName(SessionHelp.getStoreName(session)) ;
            storeIds.set(0,storeParament);
        }*/


        try {
            //疗程欠款
            if(allowdebt==1){
                if(debtUsable==null||debtRatio==null){
                    throw new SmsException(ResponseCode.NONULL);
                }
            }
            //充值欠款
            if(recDebt==1){
                if(recDebtUsable==null||recDebtRatio==null){
                    throw new SmsException(ResponseCode.NONULL);
                }
            }
            //加价转疗程欠款
            if(cardCangeListDebt==1){
                if(cardCangeListDebtUsable==null||cardCangeListRatio==null){
                    throw new SmsException(ResponseCode.NONULL);
                }
            }

            i += ssmStoreParaService.updateStorePara1(formMap,storeIds);
        } catch (SmsException e){
            log.error("SsmStoreParaController.update:", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStoreParaController.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("/updates")
    public GenericResponse updates(@RequestParam List<StoreParament> storeParaments, HttpSession session) {
        GenericResponse<StoreParament> genericResponse = new GenericResponse();

        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        int i = 0;
        try {
            i = ssmStoreParaService.updateStoreParas(storeParaments,storeId,storeName);
        } catch (SmsException e){
            log.error("SsmStoreParaController.update:", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStoreParaController.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("/fpupdate")
    public GenericResponse fpupdate(@RequestParam Map<String,Object> formMap, HttpSession session) {
        GenericResponse<StoreParament> genericResponse = new GenericResponse();

//        Long storeId = SessionHelp.getStoreId(session);
//        String storeName = SessionHelp.getStoreName(session);
        Long storeId = Long.valueOf(formMap.get("storeId").toString());
        String storeName = formMap.get("storeName").toString();
        int i = 0;
        try {
            i = ssmStoreParaService.updateStorePara(formMap,storeId,storeName);
        } catch (SmsException e){
            log.error("SsmStoreParaController.update:", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStoreParaController.update:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    //短信模板


}
