package com.hzs.bonus.bonus.controller.manage;

import com.hzs.bonus.bonus.param.BonusExpandParam;
import com.hzs.bonus.bonus.service.ICuBonusExpandService;
import com.hzs.bonus.bonus.vo.BonusExpandTitleVO;
import com.hzs.common.core.annotation.AccessPermissions;
import com.hzs.common.core.constant.HttpStatus;
import com.hzs.common.core.constant.msg.MemberMsgConstants;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.member.ext.CuBonusExpandExt;
import com.hzs.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * <p>
 * 4-n增加设置碰次 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2024-07-19
 */
@RestController
@RequestMapping("/expand")
public class CuBonusExpandController extends BaseController {

    @Autowired
    private ICuBonusExpandService bonusExpandService;

    /**
     * 在管理页面修改保存4-n参数
     *
     * @return AjaxResult
     */
    @PostMapping("/touch/save")
    public AjaxResult saveCuBonusExpand(@RequestBody BonusExpandParam bonusExpandParam) {
        if (bonusExpandParam.getAddTouch() == null) {
            return AjaxResult.error(MemberMsgConstants.REQUIRED_NOT_EMPTY);
        }
        if (bonusExpandParam.getPkId() == null && bonusExpandParam.getSettleDate() == null) {
            return AjaxResult.error(MemberMsgConstants.REQUIRED_NOT_EMPTY);
        }
        bonusExpandParam.setPkCountry(SecurityUtils.getPkCountry());
        bonusExpandParam.setPkModified(SecurityUtils.getUserId());
        bonusExpandService.saveCuBonusExpand(bonusExpandParam);
        return AjaxResult.success();
    }

    /**
     * 在管理页面修改保存4-n参数
     *
     * @return AjaxResult
     */
    @PostMapping("/touch/repeat")
    public AjaxResult repeatCuBonusByExpand(@RequestBody BonusExpandParam bonusExpandParam) {
        if (bonusExpandParam.getSettleDate() == null) {
            return AjaxResult.error(MemberMsgConstants.REQUIRED_NOT_EMPTY);
        }
        bonusExpandParam.setPkCountry(SecurityUtils.getPkCountry());
        bonusExpandService.repeatCuBonusByExpand(bonusExpandParam);
        return AjaxResult.success();
    }


    /**
     * 分页查询会员某个时间段内的奖金汇总
     */
    @AccessPermissions("ForeNumber")
    @PostMapping("/touch/query")
    public TableDataInfo listCuBonusExpand(@RequestBody BonusExpandParam bonusExpandParam) {
        if (bonusExpandParam.getSettleDate() == null) {
            TableDataInfo rspData = new TableDataInfo();
            rspData.setCode(HttpStatus.ERROR);
            rspData.setMsg("查询条件中结算日期不能为空");
            return rspData;
        }
        bonusExpandParam.setPkCountry(SecurityUtils.getPkCountry());
        startPage();
        return getDataTable(bonusExpandService.queryBonusExpand(bonusExpandParam));
    }

    /**
     * 分页查询会员某个时间段内的奖金汇总
     */
    @GetMapping("/touch/query-title")
    public TableDataInfo listCuBonusExpandTitle() {
        BonusExpandParam bonusExpandParam = BonusExpandParam.builder().pkCountry(SecurityUtils.getPkCountry()).build();
        Map<Date, CuBonusExpandExt> bonusExpandExtMap = new HashMap<>();
        List<CuBonusExpandExt> cuBonusExpandExtList = bonusExpandService.queryBonusExpandDis(bonusExpandParam);
        cuBonusExpandExtList.forEach(cuBonusExpandExt ->
                bonusExpandExtMap.put(cuBonusExpandExt.getSettleDate(), cuBonusExpandExt));
        List<BonusExpandTitleVO> bonusExpandTitleVoList = new ArrayList<>();
        for (int i = 1; i <= 6; i++) {
            Date settleDate = DateUtils.beforeDate(i, ChronoUnit.DAYS);
            BonusExpandTitleVO bonusExpandTitleVO = new BonusExpandTitleVO();
            bonusExpandTitleVO.setSettleDate(DateUtils.parseDateToFormat(DateUtils.YYYY_MM_DD, settleDate));
            bonusExpandTitleVO.setIsDisplayRed(EYesNo.NO.getIntValue());
            if(bonusExpandExtMap.containsKey(settleDate)){
                bonusExpandTitleVO.setIsDisplayRed(EYesNo.YES.getIntValue());
            }
            bonusExpandTitleVoList.add(bonusExpandTitleVO);
        }
        return getDataTable(bonusExpandTitleVoList);
    }
}
