package com.bweb.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsStore;
import com.hfkj.entity.BsStoreDiscountActivity;
import com.hfkj.model.ResponseData;
import com.hfkj.model.UserInfoModel;
import com.hfkj.service.BsStoreDiscountActivityService;
import com.hfkj.service.BsStoreService;
import com.hfkj.sysenum.SecUserTypeEnum;
import com.hfkj.sysenum.StoreDiscountActivityPartakeWayEnum;
import com.hfkj.sysenum.StoreDiscountActivityStatusEnum;
import com.hfkj.sysenum.StoreDiscountActivityTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @className: BsStoreDiscountActivityController
 * @author: HuRui
 * @date: 2022/12/4
 **/
@Controller
@RequestMapping(value = "/storeDiscountActivity")
@Api(value = "门店优惠活动")
public class BsStoreDiscountActivityController {

    private static Logger log = LoggerFactory.getLogger(BsStoreDiscountActivityController.class);

    @Resource
    private BsStoreDiscountActivityService storeDiscountActivityService;

    @Resource
    private BsStoreService storeService;

    @Resource
    private UserCenter userCenter;

    @RequestMapping(value = "/configActivity", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "配置活动")
    public ResponseData configActivity(@RequestBody BsStoreDiscountActivity body) {
        try {
            if (body == null
                    || body.getPartakeWay() == null
                    || body.getStoreId() == null
                    || StringUtils.isBlank(body.getName())
                    || body.getDiscountType() == null
                    || body.getDiscountPrice() == null
                    || body.getFinalCostPrice() == null
                    || body.getEndTime() == null
            ) {
                log.error("BsStoreDiscountActivityController --> configActivity() error!", "参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            UserInfoModel userInfoModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userInfoModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }

            StoreDiscountActivityPartakeWayEnum partakeWayEnum = StoreDiscountActivityPartakeWayEnum.getDataByNumber(body.getPartakeWay());
            if (partakeWayEnum == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的参与类型");
            }
            if (partakeWayEnum.getNumber().equals(StoreDiscountActivityPartakeWayEnum.type3.getNumber())
                && (body.getRestrictType() == null || body.getRestrictPartakeNum() == null)) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "需要填写限制参与条件");
            }

            if (body.getDiscountType().equals(StoreDiscountActivityTypeEnum.type1.getNumber())
                    && body.getDiscountCondition() == null) {
                log.error("BsStoreDiscountActivityController --> configActivity() error!", "满减需要填写满减金额");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "满减需要填写满减金额");
            }

            if (body.getId() != null) {
                BsStoreDiscountActivity activity = storeDiscountActivityService.getDetailById(body.getId());
                if (activity == null) {
                    log.error("BsStoreDiscountActivityController --> configActivity() error!", "未找到优惠活动");
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "未找到优惠活动");
                }
                body.setId(activity.getId());
                body.setCreateTime(activity.getCreateTime());
                body.setStatus(activity.getStatus());
            } else {
                body.setStatus(StoreDiscountActivityStatusEnum.status1.getNumber());
            }

            // 查询门店
            BsStore store = storeService.getStoreById(body.getStoreId());
            if (store == null) {
                log.error("BsStoreDiscountActivityController --> configActivity() error!", "未知的门店");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "未知的门店");
            }
            body.setCompanyId(store.getCompanyId());
            body.setCompanyName(store.getCompanyName());
            body.setAgentId(store.getAgentId());
            body.setAgentName(store.getAgentName());
            body.setStoreNo(store.getStoreNo());
            body.setStoreName(store.getName());
            body.setStoreTel(store.getTelephone());
            body.setOperatorId(userInfoModel.getSecUser().getId());
            body.setOperatorName(userInfoModel.getSecUser().getUserName());
            return ResponseMsgUtil.success(storeDiscountActivityService.editActivity(body));

        } catch (Exception e) {
            log.error("BsStoreDiscountActivityController --> configActivity() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getActivityDetail", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询活动详情")
    public ResponseData getActivityDetail(@RequestParam(value = "activityId" , required = true) Long activityId) {
        try {

            return ResponseMsgUtil.success(storeDiscountActivityService.getDetailById(activityId));

        } catch (Exception e) {
            log.error("BsStoreDiscountActivityController --> getActivityDetail() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/upOnline", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "上线")
    public ResponseData upOnline(@RequestParam(value = "activityId" , required = true) Long activityId) {
        try {

            BsStoreDiscountActivity activity = storeDiscountActivityService.getDetailById(activityId);
            if (activity == null) {
                log.error("BsStoreDiscountActivityController --> upOnline() error!", "未找到优惠活动");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "未找到优惠活动");
            }
            activity.setStatus(StoreDiscountActivityStatusEnum.status2.getNumber());
            return ResponseMsgUtil.success(storeDiscountActivityService.editActivity(activity));

        } catch (Exception e) {
            log.error("BsStoreDiscountActivityController --> upOnline() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/lowerOnline", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "下线活动")
    public ResponseData lowerOnline(@RequestParam(value = "activityId" , required = true) Long activityId) {
        try {
            BsStoreDiscountActivity activity = storeDiscountActivityService.getDetailById(activityId);
            if (activity == null) {
                log.error("BsStoreDiscountActivityController --> lowerOnline() error!", "未找到优惠活动");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "未找到优惠活动");
            }
            activity.setStatus(StoreDiscountActivityStatusEnum.status3.getNumber());
            return ResponseMsgUtil.success(storeDiscountActivityService.editActivity(activity));

        } catch (Exception e) {
            log.error("BsStoreDiscountActivityController --> lowerOnline() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "删除")
    public ResponseData delete(@RequestParam(value = "activityId" , required = true) Long activityId) {
        try {
            BsStoreDiscountActivity activity = storeDiscountActivityService.getDetailById(activityId);
            if (activity == null) {
                log.error("BsStoreDiscountActivityController --> lowerOnline() error!", "未找到优惠活动");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "未找到优惠活动");
            }
            activity.setStatus(StoreDiscountActivityStatusEnum.status0.getNumber());
            return ResponseMsgUtil.success(storeDiscountActivityService.editActivity(activity));

        } catch (Exception e) {
            log.error("BsStoreDiscountActivityController --> lowerOnline() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getActivityList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询活动列表")
    public ResponseData getActivityList(@RequestParam(value = "companyId" , required = false) Long companyId,
                                        @RequestParam(value = "merId" , required = false) Long merId,
                                        @RequestParam(value = "storeId" , required = false) Long storeId,
                                        @RequestParam(value = "name" , required = false) String name,
                                        @RequestParam(value = "status" , required = false) Integer status,
                                        @RequestParam(value = "pageNum" , required = true) Integer pageNum,
                                        @RequestParam(value = "pageSize" , required = true) Integer pageSize) {
        try {
            UserInfoModel userInfoModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userInfoModel == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.SEC_USER_EXPIRED, "");
            }
            Map<String,Object> param = new HashMap<>();
            param.put("name",name);
            param.put("status",status);

            if (userInfoModel.getSecUser().getObjectType().equals(SecUserTypeEnum.type0.getNumber())) {
                param.put("companyId", companyId);
                param.put("merId", merId);
                param.put("storeId", storeId);

            } else if (userInfoModel.getSecUser().getObjectType().equals(SecUserTypeEnum.type1.getNumber())) {
                param.put("merId", merId);
                param.put("storeId", storeId);

            } else if (userInfoModel.getSecUser().getObjectType().equals(SecUserTypeEnum.type4.getNumber())) {
                param.put("merId", userInfoModel.getMer().getId());
                param.put("storeId", storeId);

            } else if (userInfoModel.getSecUser().getObjectType().equals(SecUserTypeEnum.type5.getNumber())) {
                param.put("storeId", userInfoModel.getStore().getId());

            } else {
                log.error("BsStoreDiscountActivityController --> getActivityList() error!", "该主角色没有权限");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.MENU_TREE_HAS_NOT_ERROR, "");
            }
            PageHelper.startPage(pageNum, pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(storeDiscountActivityService.getActivityList(param)));

        } catch (Exception e) {
            log.error("BsStoreDiscountActivityController --> getActivityList() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
