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.util.SessionHelp;
import com.ssm.manage.entity.PromotionList;
import com.ssm.manage.entity.PromotionMember;
import com.ssm.manage.entity.PromotionProject;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.PromotionListRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmPromotionMemberService;
import com.ssm.manage.service.SsmPromotionProjectService;
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.util.ArrayList;
import java.util.List;

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

    @Autowired
    private SsmPromotionProjectService ssmPromotionProjectService;

    @Autowired
    private SsmPromotionMemberService ssmPromotionMemberService;

    @ApiOperation(value = "分页查询  门店端-促销活动-分页查询促销活动", httpMethod = "POST", response = PageResponse.class)
    @RequestMapping("/page")
    public PageResponse selectPage(@RequestBody PromotionListRequest promotionListRequest) {
        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 = GenericResponse.class)
    @RequestMapping("/proList")
    public GenericResponse proList(@RequestBody PromotionListRequest promotionListRequest, HttpSession session) {
        GenericResponse pageResponse = new GenericResponse();
        promotionListRequest.setStoreId(SessionHelp.getStoreId(session));
        List<PromotionProject> list = new ArrayList<>();
        try {
            list = ssmPromotionProjectService.selectList(promotionListRequest);
        } catch (Exception e) {
            log.error("SsmPromotionController.proList", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setRows(list);
        return pageResponse;
    }

    @ApiOperation(value = "分页查询  门店端-促销活动-分页查询促销活动指定会员数据", httpMethod = "POST", response = PageResponse.class)
    @RequestMapping("/memberPage")
    public PageResponse memberPage(@RequestBody PromotionListRequest promotionListRequest) {
        PageResponse<PromotionMember> pageResponse = new PageResponse<>();
        PageInfo<PromotionMember> pageInfo = new PageInfo<>();
//        promotionListRequest.setStoreId(SessionHelp.getStoreId(session));
        try {
            pageInfo = ssmPromotionMemberService.selectPage(promotionListRequest);
        } catch (Exception e) {
            log.error("SsmPromotionController.memberPage", 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) {
        GenericResponse genericResponse = new 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().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("/update")
    public GenericResponse update(@RequestBody PromotionListRequest promotionListRequest) {
        GenericResponse genericResponse = new GenericResponse();

//        String staffPhone = SessionHelp.getLoginAccount(session);
//        promotionListRequest.setStaffPhone(staffPhone);
        int i = 0;
        try {
            for(PromotionProject promotionProject:promotionListRequest.getPromotionProjectLists()){
                if(promotionProject.getValidTime().longValue()<0){
                    throw new SmsException("010000","操作失败，不能输入负数");
                }
            }
            if(promotionListRequest.getCardSalesCommission().longValue()<0||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("/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) {
        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) {
        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;
    }

}
