package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.response.PageResponse;
import com.ssm.manage.utils.SessionHelp;
import com.ssm.core.utils.BigDecimalUtils;
import com.ssm.manage.entity.PromotionList;
import com.ssm.manage.entity.PromotionProject;
import com.ssm.manage.entity.PromotionRechargeList;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.PromotionListRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmPromotionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
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.math.BigDecimal;
import java.util.*;

@Api(value = "促销Controller",tags = {"促销相关接口"})
@RestController
@RequestMapping("/demay/ssm/promotion")
@Slf4j
public class SsmPromotionController {
    @Autowired
    private SsmPromotionService ssmPromotionService;

    @ApiOperation(value = "分页查询  门店端-促销活动-分页查询促销活动", httpMethod = "POST", response = PageResponse.class)
    @RequestMapping("/page")
    public PageResponse selectPage(@RequestBody PromotionListRequest promotionListRequest, HttpSession session) {
        PageResponse<PromotionListRequest> pageResponse = new PageResponse<>();
        PageInfo<PromotionListRequest> pageInfo = new PageInfo<>();
        promotionListRequest.setStoreId(SessionHelp.getStoreId(session));
        try {
            pageInfo = ssmPromotionService.selectPage(promotionListRequest);
        } catch (Exception e) {
            log.error("SsmPromotionController.selectPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }




    @ApiOperation(value = "查询所有  门店端-促销活动-查询促销活动", httpMethod = "POST", response = PageResponse.class)
    @RequestMapping("/selectList")
    public PageResponse selectList(@RequestBody PromotionListRequest promotionListRequest, HttpSession session) {
        PageResponse<Map<String,Object>> pageResponse = new PageResponse<>();
        List<Map<String,Object>> pageInfo = new ArrayList<>();
        promotionListRequest.setStoreId(SessionHelp.getStoreId(session));
        try {
            pageInfo = ssmPromotionService.selectList(promotionListRequest);
        } catch (Exception e) {
            log.error("SsmPromotionController.selectPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setData(pageInfo);
        return pageResponse;
    }

    @ApiOperation(value = "分页查询  门店端-促销活动-分页查询促销活动(升级后设置促销使用门店)", httpMethod = "POST", response = PageResponse.class)
    @RequestMapping("/pages")
    public PageResponse selectPages(@RequestBody PromotionListRequest promotionListRequest, HttpSession session) {
        PageResponse<PromotionList> pageResponse = new PageResponse<>();
        PageInfo<PromotionList> pageInfo = new PageInfo<>();
        promotionListRequest.setStoreId(SessionHelp.getStoreId(session));
        try {
            pageInfo = ssmPromotionService.selectPages(promotionListRequest);
        } catch (Exception e) {
            log.error("SsmPromotionController.selectPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    @ApiOperation(value = "分页查询  门店端-促销活动-分页查询促销活动", httpMethod = "POST", response = PageResponse.class)
    @RequestMapping("/pagenew")
    public PageResponse selectPagenew(@RequestBody PromotionListRequest promotionListRequest, HttpSession session) {
        PageResponse<PromotionList> pageResponse = new PageResponse<>();
        PageInfo<PromotionList> pageInfo = new PageInfo<>();
        promotionListRequest.setStoreId(SessionHelp.getStoreId(session));
        try {
            pageInfo = ssmPromotionService.selectPagenew(promotionListRequest);
        } catch (Exception e) {
            log.error("SsmPromotionController.selectPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }



    @ApiOperation(value = "促销活动添加  (门店端-促销活动-促销活动添加)", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/add")
    public GenericResponse add(@RequestBody PromotionListRequest promotionListRequest, HttpSession session) {
//todo  进行判断，如果项目中有单价为零的，就推荐使用买就送的促销
        GenericResponse genericResponse = new GenericResponse();
        if(promotionListRequest.getCardBuy()==null){
            promotionListRequest.setCardBuy(1);
        }
        List<PromotionProject> promotionProjectLists = promotionListRequest.getPromotionProjectLists();
        if(promotionListRequest.getType()==0) {
            for (PromotionProject project : promotionProjectLists) {
                if (project.getProjectTotalMoney().compareTo(BigDecimalUtils.bigDecimal("0")) == 0) {
                    genericResponse.setCodeAndMsg(ResponseCode.PROMATION_is_ZERO);
                    return genericResponse;
                }
            }
        }
        if(promotionListRequest.getType()==2 && promotionListRequest.getProjectTotal()<promotionListRequest.getProjectCount()){
            genericResponse.setCodeAndMsg(ResponseCode.MSG,"可选项目数量大于项目总数");
            return genericResponse;
        }
        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        String staffPhone = SessionHelp.getLoginAccount(session);
        promotionListRequest.setStoreId(storeId);
        promotionListRequest.setStoreName(storeName);
        promotionListRequest.setStaffPhone(staffPhone);
        int i = 0;
        try {
            for(PromotionProject promotionProject:promotionListRequest.getPromotionProjectLists()){
                if(promotionProject.getValidTime()!=null&&promotionProject.getValidTime().longValue()<0){
                    throw new SmsException("010000","操作失败，不能输入负数");
                }
            }
            if(promotionListRequest.getCardSalesCommission().longValue()<0||promotionListRequest.getSalesCommission().longValue()<0){
                throw new SmsException("010000","操作失败，不能输入负数");
            }
            i = ssmPromotionService.addPromotion(promotionListRequest);
        } catch (SmsException e) {
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e) {
            log.error("SsmPromotionController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "促销活动添加  (门店端-促销活动-促销活动添加)", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/addCourseGitRechargePromotion")
    public GenericResponse addCourseGitRechargePromotion(@RequestBody PromotionListRequest promotionListRequest, HttpSession session) {
//todo  进行判断，如果项目中有单价为零的，就推荐使用买就送的促销
        GenericResponse genericResponse = new GenericResponse();
        if(promotionListRequest.getCardBuy()==null){
            promotionListRequest.setCardBuy(1);
        }
        List<PromotionProject> promotionProjectLists = promotionListRequest.getPromotionProjectLists();
        if(promotionListRequest.getType()==0) {
            for (PromotionProject project : promotionProjectLists) {
                if (project.getProjectTotalMoney().compareTo(BigDecimalUtils.bigDecimal("0")) == 0) {
                    genericResponse.setCodeAndMsg(ResponseCode.PROMATION_is_ZERO);
                    return genericResponse;
                }
            }
        }
        //判断疗程赠送充值卡的充值卡金额是否为零
        if (promotionListRequest.getType()==3){
            List<PromotionRechargeList> promotionRechargeLists = promotionListRequest.getPromotionRechargeLists();
            for (PromotionRechargeList promotionRechargeList : promotionRechargeLists) {
                if(promotionRechargeList.getRechargeMoney().compareTo(BigDecimal.ZERO)!=1){
                    genericResponse.setCodeAndMsg(ResponseCode.PROMATION_RECHARGE_MONEY);
                    return genericResponse;
                }
            }
        }
        if(promotionListRequest.getType()==2 && promotionListRequest.getProjectTotal()<promotionListRequest.getProjectCount()){
            genericResponse.setCodeAndMsg(ResponseCode.MSG,"可选项目数量大于项目总数");
            return genericResponse;
        }
        Long storeId = SessionHelp.getStoreId(session);
        String storeName = SessionHelp.getStoreName(session);
        String staffPhone = SessionHelp.getLoginAccount(session);
        promotionListRequest.setStoreId(storeId);
        promotionListRequest.setStoreName(storeName);
        promotionListRequest.setStaffPhone(staffPhone);
        int i = 0;
        try {
            for(PromotionProject promotionProject:promotionListRequest.getPromotionProjectLists()){
                if(promotionProject.getValidTime()!=null&&promotionProject.getValidTime().longValue()<0){
                    throw new SmsException("010000","操作失败，不能输入负数");
                }
            }
            if(promotionListRequest.getCardSalesCommission().longValue()<0||promotionListRequest.getSalesCommission().longValue()<0){
                throw new SmsException("010000","操作失败，不能输入负数");
            }
            i = ssmPromotionService.addCourseGitRechargePromotion(promotionListRequest);
        } catch (SmsException e) {
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e) {
            log.error("SsmPromotionController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "促销活动修改  (门店端-促销活动-促销活动修改)", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/update")
    public GenericResponse update(@RequestBody PromotionListRequest promotionListRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();

        if(promotionListRequest.getType()==2 && promotionListRequest.getProjectTotal()<promotionListRequest.getProjectCount()){
            genericResponse.setCodeAndMsg(ResponseCode.MSG,"可选项目数量大于项目总数");
            return genericResponse;
        }
        String staffPhone = SessionHelp.getLoginAccount(session);
        promotionListRequest.setStaffPhone(staffPhone);

        int i = 0;
        try {
            for(PromotionProject promotionProject:promotionListRequest.getPromotionProjectLists()){
                if(promotionProject.getValidTime()!=null&&promotionProject.getValidTime().longValue()<0){
                    throw new SmsException("010000","操作失败，不能输入负数");
                }
            }
            if((promotionListRequest.getCardSalesCommission()!=null&&promotionListRequest.getCardSalesCommission().longValue()<0)||(promotionListRequest.getSalesCommission()!=null&&promotionListRequest.getSalesCommission().longValue()<0)){
                throw new SmsException("010000","操作失败，不能输入负数");
            }
            i = ssmPromotionService.updatePromotion(promotionListRequest);
        } catch (SmsException e) {
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e) {
            log.error("SsmPromotionController.update:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "促销活动修改  (门店端-促销活动-促销活动修改)", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/updatePromotionRechargeList")
    public GenericResponse updatePromotionRechargeList(@RequestBody PromotionListRequest promotionListRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();

        if(promotionListRequest.getType()==2 && promotionListRequest.getProjectTotal()<promotionListRequest.getProjectCount()){
            genericResponse.setCodeAndMsg(ResponseCode.MSG,"可选项目数量大于项目总数");
            return genericResponse;
        }
        String staffPhone = SessionHelp.getLoginAccount(session);
        promotionListRequest.setStaffPhone(staffPhone);

        int i = 0;
        try {
            for(PromotionProject promotionProject:promotionListRequest.getPromotionProjectLists()){
                if(promotionProject.getValidTime()!=null&&promotionProject.getValidTime().longValue()<0){
                    throw new SmsException("010000","操作失败，不能输入负数");
                }
            }
            if((promotionListRequest.getCardSalesCommission()!=null&&promotionListRequest.getCardSalesCommission().longValue()<0)||(promotionListRequest.getSalesCommission()!=null&&promotionListRequest.getSalesCommission().longValue()<0)){
                throw new SmsException("010000","操作失败，不能输入负数");
            }
            i = ssmPromotionService.updatePromotionRechargeList(promotionListRequest);
        } catch (SmsException e) {
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e) {
            log.error("SsmPromotionController.update:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "促销活动送审  (门店端-促销活动-促销活动送审)", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/sendAudit")
    public GenericResponse sendAudit(@RequestBody PromotionListRequest promotionListRequest) {
        GenericResponse genericResponse = new GenericResponse();
        int i = 0;
        try {
            i = ssmPromotionService.sendAudit(promotionListRequest);
        }catch (SmsException e) {
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmPromotionController.sendAudit:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "促销活动回收  (门店端-促销活动-促销活动回收)", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/revokeAudit")
    public GenericResponse revokeAudit(@RequestBody PromotionListRequest promotionListRequest) {
        GenericResponse genericResponse = new GenericResponse();
        int i = 0;
        try {
            i = ssmPromotionService.revokeAudit(promotionListRequest);
        }catch (SmsException e) {
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmPromotionController.revokeAudit:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }



    @ApiOperation(value = "促销活动审核  (门店端-促销活动-促销活动审核)", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/audit")
    public GenericResponse audit(@RequestBody PromotionListRequest promotionListRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        promotionListRequest.setStaffPhone(SessionHelp.getLoginAccount(session));
        int i = 0;
        try {
            i = ssmPromotionService.audit(promotionListRequest);
        }
//        catch (SmsException e) {
//            genericResponse.setCodeAndMsg(e);
//            return genericResponse;
//        }
        catch (Exception e) {
            log.error("SsmPromotionController.audit:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }


    @ApiOperation(value = "促销活动反审核  (门店端-促销活动-促销活动反审核)", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/unAudit")
    public GenericResponse unAudit(@RequestBody PromotionListRequest promotionListRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        promotionListRequest.setStaffPhone(SessionHelp.getLoginAccount(session));
        int i = 0;
        try {
            i = ssmPromotionService.unAudit(promotionListRequest);
        }
//        catch (SmsException e) {
//            genericResponse.setCodeAndMsg(e);
//            return genericResponse;
//        }
        catch (Exception e) {
            log.error("SsmPromotionController.unAudit:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }


    @ApiOperation(value = "促销活动推送  (门店端-促销活动-促销活动推送)", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/push")
    public GenericResponse push(@RequestBody PromotionListRequest promotionListRequest) {
        GenericResponse genericResponse = new GenericResponse();
        int i = 0;
        try {
            i = ssmPromotionService.push(promotionListRequest);
        }
//        catch (SmsException e) {
//            genericResponse.setCodeAndMsg(e);
//            return genericResponse;
//        }
        catch (Exception e) {
            log.error("SsmPromotionController.push:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }


    @ApiOperation(value = "促销活动关闭  (门店端-促销活动-促销活动关闭)", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/shutDown")
    public GenericResponse shutDown(@RequestBody PromotionListRequest promotionListRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        promotionListRequest.setStaffPhone(SessionHelp.getLoginAccount(session));
        int i = 0;
        try {
            i = ssmPromotionService.shutDown(promotionListRequest);
        } catch (SmsException e) {
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }
        catch (Exception e) {
            log.error("SsmPromotionController.shutDown:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "查询促销类型  (门店端-促销活动-促销活动关闭)", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/selectPromotionType")
    public GenericResponse<List<PromotionList>> selectPromotionType(@RequestBody PromotionListRequest promotionListRequest, HttpSession session) {
        GenericResponse<List<PromotionList>> genericResponse = new GenericResponse();
        int i = 0;
        try {
           List<PromotionList> promotionLists   = ssmPromotionService.selectPromotionType(promotionListRequest);
           genericResponse.setData(promotionLists);
        } catch (Exception e) {
            log.error("SsmPromotionController.shutDown:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }


}
