package com.hzs.activity.other.controller.manage;/**
 * @Description:
 * @Author: yuhui
 * @Time: 2024/1/18 9:52
 * @Classname: OtherPriceConfigController
 * @PackageName: com.hzs.activity.other.controller
 */
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.activity.base.param.ActivityParam;
import com.hzs.activity.base.service.IAcBaseConfigService;
import com.hzs.activity.other.param.OtherPriceConfigQueryParam;
import com.hzs.activity.other.service.IAcOtherPriceConfigService;
import com.hzs.activity.policy.param.PolicyConfigQueryParam;
import com.hzs.activity.policy.vo.AcPolicyConfigVo;
import com.hzs.common.core.constant.msg.CommonMsgConstants;
import com.hzs.common.core.enums.EActType;
import com.hzs.common.core.enums.EActivityStatus;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.activity.base.AcBaseConfig;
import com.hzs.common.domain.activity.other.AcOtherPriceConfig;
import com.hzs.common.security.service.UserTokenService;
import com.hzs.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 *@BelongsProject: hzs_java_cloud_africa
 *@BelongsPackage: com.hzs.activity.other.controller
 *@Author: yh
 *@CreateTime: 2024-01-18  09:52
 *@Description: TODO
 *@Version: 1.0
 */
@RestController
@RequestMapping("/manage/other-price-config")
public class OtherPriceConfigController extends BaseController {
    @Autowired
    private IAcBaseConfigService baseConfigService;
    @Autowired
    private UserTokenService userTokenService;
    @Autowired
    private IAcOtherPriceConfigService otherPriceConfigService;
    /**
     * 查询全部
     * @return
     */
    @GetMapping("list")
    public AjaxResult list(PolicyConfigQueryParam policyConfigQueryParam){
        Integer pkCountry = SecurityUtils.getPkCountry();
//        R<CurrencyDTO> currencyDTO = currencyServiceApi.getCurrency(pkCountry);
//        CurrencyDTO currency = currencyDTO.getData();
        QueryWrapper<AcBaseConfig> queryWrapper = new QueryWrapper<AcBaseConfig>();
        queryWrapper.eq("PK_COUNTRY", pkCountry);
        queryWrapper.eq("ACT_TYPE", policyConfigQueryParam.getActType());
        if (StringUtils.isNotBlank(policyConfigQueryParam.getActName())){
            queryWrapper.like("ACT_NAME", policyConfigQueryParam.getActName());
        }
        if (policyConfigQueryParam.getStartActStartDate() != null && policyConfigQueryParam.getEndActStartDate() != null){
            queryWrapper.between("ACT_START_DATE", policyConfigQueryParam.getStartActStartDate(),
                    policyConfigQueryParam.getEndActStartDate());
        }
        if (policyConfigQueryParam.getStartActEndDate() != null && policyConfigQueryParam.getEndActEndDate() != null){
            queryWrapper.between("ACT_END_DATE", policyConfigQueryParam.getStartActEndDate(),
                    policyConfigQueryParam.getEndActEndDate());
        }
        // 创建时间
        if (policyConfigQueryParam.getStartCreateDate() != null && policyConfigQueryParam.getEndCreateDate() != null){
            queryWrapper.between("CREATION_TIME", policyConfigQueryParam.getStartCreateDate(),
                    policyConfigQueryParam.getEndCreateDate());
        }
        // 自定义条件
        String condition = "";
        if (policyConfigQueryParam.getStatus() != null){
            // 活动状态
            if (policyConfigQueryParam.getStatus().equals(EActivityStatus.NOT_STARTED.getValue())){
                condition = "  (  (SELECT SYSDATE FROM DUAL) < ACT_START_DATE     )  ";
            }else if (policyConfigQueryParam.getStatus().equals(EActivityStatus.IN_PROGRESS.getValue())){
                condition ="  (   ((SELECT SYSDATE FROM DUAL) > ACT_START_DATE  and (to_date(to_char(SYSDATE, 'yyyy-MM-dd hh:mi:ss'), 'yyyy-MM-dd hh:mi:ss')) <= ACT_END_DATE )    )";
            }else if (policyConfigQueryParam.getStatus().equals(EActivityStatus.EXPIRED.getValue())){
                condition ="  (    (to_date(to_char(SYSDATE, 'yyyy-MM-dd hh:mi:ss'), 'yyyy-MM-dd hh:mi:ss')) > ACT_END_DATE  )";
            }
            // 组装条件
            queryWrapper.apply(condition);
        }
        List<AcBaseConfig> baseConfigList = baseConfigService.list(queryWrapper);
        List<AcPolicyConfigVo> resultList = baseConfigList.stream().map(a -> {
            AcPolicyConfigVo acBaseConfigExt = BeanUtil.copyProperties(a, AcPolicyConfigVo.class);
            // 判断活动状态
            // 开始时间
            long startTime = a.getActStartDate().getTime();
            // 结束时间
            long endTime = a.getActEndDate().getTime();
            // 当前时间
            long currentTime = DateUtils.currentDateTime().getTime();
            if ((currentTime > startTime && currentTime <= endTime) ) {
                // 当前时间大于开始 小于结束
                acBaseConfigExt.setStatus(EActivityStatus.IN_PROGRESS.getLabel());
            } else if (currentTime < startTime) {
                // 当前时间小于开始时间
                acBaseConfigExt.setStatus(EActivityStatus.NOT_STARTED.getLabel());
            } else if (currentTime > endTime) {
                // 当前时间大于结束时间
                acBaseConfigExt.setStatus(EActivityStatus.EXPIRED.getLabel());
            }
            return acBaseConfigExt;
        }).collect(Collectors.toList());


        return AjaxResult.success(resultList);
    }


    /**
     * 查询详情
     */
    @GetMapping("detail")
    public AjaxResult detail(Long pkBaseId){
        AcBaseConfig baseConfig = baseConfigService.getById(pkBaseId);
        ActivityParam activityParam = BeanUtil.copyProperties(baseConfig, ActivityParam.class);

        QueryWrapper<AcOtherPriceConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_BASE_ID",pkBaseId);
        List<AcOtherPriceConfig> otherPriceConfigList = otherPriceConfigService.list(queryWrapper);
        activityParam.setOtherPriceConfigList(otherPriceConfigList);
        return AjaxResult.success(activityParam);
    }

    /**
     * 查询全部
     * @return
     */
    @PostMapping("export")
    public void export(PolicyConfigQueryParam policyConfigQueryParam, HttpServletResponse response){
        Integer pkCountry = SecurityUtils.getPkCountry();
//        R<CurrencyDTO> currencyDTO = currencyServiceApi.getCurrency(pkCountry);
//        CurrencyDTO currency = currencyDTO.getData();
        QueryWrapper<AcBaseConfig> queryWrapper = new QueryWrapper<AcBaseConfig>();
        queryWrapper.eq("PK_COUNTRY", pkCountry);
        queryWrapper.eq("ACT_TYPE", policyConfigQueryParam.getActType());
        if (StringUtils.isNotBlank(policyConfigQueryParam.getActName())){
            queryWrapper.like("ACT_NAME", policyConfigQueryParam.getActName());
        }
        if (policyConfigQueryParam.getStartActStartDate() != null && policyConfigQueryParam.getEndActStartDate() != null){
            queryWrapper.between("ACT_START_DATE", DateUtils.getStartTime(policyConfigQueryParam.getStartActStartDate()),
                    DateUtils.getEndTime(policyConfigQueryParam.getEndActStartDate()));
        }
        if (policyConfigQueryParam.getStartActEndDate() != null && policyConfigQueryParam.getEndActEndDate() != null){
            queryWrapper.between("ACT_END_DATE", DateUtils.getStartTime(policyConfigQueryParam.getStartActEndDate()),
                    DateUtils.getEndTime(policyConfigQueryParam.getEndActEndDate()));
        }
        // 创建时间
        if (policyConfigQueryParam.getStartCreateDate() != null && policyConfigQueryParam.getEndCreateDate() != null){
            queryWrapper.between("CREATION_TIME", DateUtils.getStartTime(policyConfigQueryParam.getStartCreateDate()),
                    DateUtils.getEndTime(policyConfigQueryParam.getEndCreateDate()));
        }
        // 自定义条件
        String condition = "";
        if (policyConfigQueryParam.getStatus() != null){
            // 活动状态
            if (policyConfigQueryParam.getStatus().equals(EActivityStatus.NOT_STARTED.getValue())){
                condition = "  (  (SELECT SYSDATE FROM DUAL) < ACT_START_DATE     )  ";
            }else if (policyConfigQueryParam.getStatus().equals(EActivityStatus.IN_PROGRESS.getValue())){
                condition ="  (   ((SELECT SYSDATE FROM DUAL) > ACT_START_DATE  and (to_date(to_char(SYSDATE, 'yyyy-MM-dd hh:mi:ss'), 'yyyy-MM-dd hh:mi:ss')) <= ACT_END_DATE )    )";
            }else if (policyConfigQueryParam.getStatus().equals(EActivityStatus.EXPIRED.getValue())){
                condition ="  (    (to_date(to_char(SYSDATE, 'yyyy-MM-dd hh:mi:ss'), 'yyyy-MM-dd hh:mi:ss')) > ACT_END_DATE  )";
            }
            // 组装条件
            queryWrapper.apply(condition);
        }
        List<AcBaseConfig> baseConfigList = baseConfigService.list(queryWrapper);
        List<AcPolicyConfigVo> resultList = baseConfigList.stream().map(a -> {
            AcPolicyConfigVo acBaseConfigExt = BeanUtil.copyProperties(a, AcPolicyConfigVo.class);
            // 判断活动状态
            // 开始时间
            long startTime = a.getActStartDate().getTime();
            // 结束时间
            long endTime = a.getActEndDate().getTime();
            // 当前时间
            long currentTime = DateUtils.currentDateTime().getTime();
            if ((currentTime > startTime && currentTime <= endTime) ) {
                // 当前时间大于开始 小于结束
                acBaseConfigExt.setStatus(EActivityStatus.IN_PROGRESS.getLabel());
            } else if (currentTime < startTime) {
                // 当前时间小于开始时间
                acBaseConfigExt.setStatus(EActivityStatus.NOT_STARTED.getLabel());
            } else if (currentTime > endTime) {
                // 当前时间大于结束时间
                acBaseConfigExt.setStatus(EActivityStatus.EXPIRED.getLabel());
            }
            return acBaseConfigExt;
        }).collect(Collectors.toList());
        if (policyConfigQueryParam.getActType().equals(EActType.CAR.getValue())){
            ExcelUtil<AcPolicyConfigVo> util = new ExcelUtil<>(AcPolicyConfigVo.class);
            util.exportExcel(response, resultList, "车奖导出");
        }else  {
            ExcelUtil<AcPolicyConfigVo> util = new ExcelUtil<>(AcPolicyConfigVo.class);
            util.exportExcel(response, resultList, "旅游奖导出");
        }

//        ExcelUtil<AcPolicyConfigVo> util = new ExcelUtil<>(AcPolicyConfigVo.class);
//        util.exportExcel(response, resultList, "政策币赠送导出");
    }

    /**
     * 提交审批
     * @param param
     * @return
     */
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody OtherPriceConfigQueryParam param) {

        if (null == param.getSignType() || CollectionUtil.isEmpty(param.getUserIdList())||
                CollectionUtil.isEmpty(param.getOtherPriceConfigList())
        ) {
            // 缺少参数
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }
        return AjaxResult.success(otherPriceConfigService.otherPriceConfigSubmit(param,userTokenService.getLoginUser()));
    }
}
