package com.hzs.activity.other.controller.api;

import cn.hutool.core.collection.CollectionUtil;
import com.hzs.activity.base.service.IAcBaseConfigService;
import com.hzs.activity.other.service.IAcOtherPriceDataService;
import com.hzs.activity.other.service.IAcOtherPriceService;
import com.hzs.activity.other.vo.AcOtherDataVO;
import com.hzs.activity.other.vo.AcOtherDetailVO;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.activity.base.ext.AcBaseConfigExt;
import com.hzs.common.domain.activity.other.AcOtherPrice;
import com.hzs.common.domain.activity.other.AcOtherPriceConfig;
import com.hzs.common.domain.activity.other.AcOtherPriceData;
import com.hzs.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 其它活动奖项控制器
 * @Author: jiang chao
 * @Time: 2024/1/18 15:15
 * @Classname: ApiAcOtherController
 * @PackageName: com.hzs.activity.other.controller.api
 */
@RestController
@RequestMapping("/api/other")
public class ApiAcOtherController {

    @Autowired
    private IAcBaseConfigService iAcBaseConfigService;
    @Autowired
    private IAcOtherPriceDataService iAcOtherPriceDataService;
    @Autowired
    private IAcOtherPriceService iAcOtherPriceService;

    /**
     * 查询活动进度
     *
     * @return
     */
    @GetMapping("/other-detail/{pkId}")
    public AjaxResult otherDetail(@PathVariable("pkId") Long pkId) {
        // 查询活动基础数据
        AcBaseConfigExt acBaseConfig = iAcBaseConfigService.queryBaseAndOther(pkId);
        if (null == acBaseConfig || CollectionUtil.isEmpty(acBaseConfig.getOtherPriceConfigList())) {
            // 数据不存在
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_OBJECT_NOT_REPEAT));
        }

        // 规则配置
        AcOtherPriceConfig acOtherPriceConfig = acBaseConfig.getOtherPriceConfigList().get(0);
        // 业绩明细列表
        List<AcOtherDetailVO> detailList = new ArrayList<>();
        // 会员ID
        Long pkMember = SecurityUtils.getUserId();

        // 查询活动业绩数据
        List<AcOtherPriceData> dataList = iAcOtherPriceDataService.queryMemberOtherData(pkId, pkMember);
        if (CollectionUtil.isNotEmpty(dataList)) {
            for (AcOtherPriceData acOtherPriceData : dataList) {
                // 未完成业绩
                BigDecimal notSumPv = BigDecimal.ZERO;
                // 是否达标
                int complianceFlag = EYesNo.YES.getIntValue();

                if (acOtherPriceData.getSumPv().compareTo(BigDecimal.ZERO) < 0) {
                    acOtherPriceData.setSumPv(BigDecimal.ZERO);
                }

                // 如果 已完成业绩 小于 总达标业绩
                if (acOtherPriceData.getSumPv().compareTo(acOtherPriceConfig.getPlaceDeptPv()) < 0) {
                    notSumPv = acOtherPriceConfig.getPlaceDeptPv().subtract(acOtherPriceData.getSumPv());
                    complianceFlag = EYesNo.NO.getIntValue();
                }

                detailList.add(AcOtherDetailVO.builder()
                        .placeDept(acOtherPriceData.getPlaceDept())
                        .placeDeptPv(acOtherPriceConfig.getPlaceDeptPv())
                        .sumPv(acOtherPriceData.getSumPv())
                        .notSumPv(notSumPv)
                        .complianceFlag(complianceFlag)
                        .build());
            }
        }

        // 查询会员直推业绩
        AcOtherPrice acOtherPrice = iAcOtherPriceService.queryMemberOrderPrice(acBaseConfig.getPkId(), pkMember);

        // 返回数据
        AcOtherDataVO acOtherDataVO = AcOtherDataVO.builder()
                .actName(acBaseConfig.getActName())
                .actStartDate(acBaseConfig.getActStartDate())
                .actEndDate(acBaseConfig.getActEndDate())
                .residueDay(DateUtils.differenceDay(acBaseConfig.getActEndDate(), new Date()) + 1)
                .detailList(detailList)
                .orderAchieve(acOtherPrice != null ? acOtherPrice.getParentPv() : BigDecimal.ZERO)
                .build();

        return AjaxResult.success(acOtherDataVO);
    }

}
