package com.hzs.activity.base.controller.manage;/**
 * @Description:
 * @Author: yuhui
 * @Time: 2023/4/20 13:51
 * @Classname: ActivityController
 * @PackageName: com.hzs.activity.controller.manage
 */

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.activity.base.param.QueryActivityParam;
import com.hzs.activity.base.vo.BaseConfigVo;
import com.hzs.activity.base.service.IActivityService;
import com.hzs.activity.base.service.IAcBaseConfigService;
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.poi.ExcelUtil;
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.activity.base.AcBaseConfig;
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.ArrayList;
import java.util.List;

/**
 * @BelongsProject: hzs_cloud
 * @BelongsPackage: com.hzs.activity.controller.manage
 * @Author: yh
 * @CreateTime: 2023-04-20  13:51
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping("/manage/activity")
public class ActivityController extends BaseController {


    @Autowired
    private IAcBaseConfigService baseConfigService;

    @Autowired
    private IActivityService IActivityService;

    /**
     * 查询活动列表
     *
     * @return
     */
    @GetMapping("list")
    public TableDataInfo activityList(QueryActivityParam queryActivityParam) {

        startPage();
        List<BaseConfigVo> resultList = IActivityService.activityList(queryActivityParam);
        return getDataTable(resultList);
    }
    /**
     * 查询全部活动
     *
     * @return
     */
    @GetMapping("find-all")
    public AjaxResult findAll(QueryActivityParam queryActivityParam) {
        List<BaseConfigVo> resultList = IActivityService.activityList(queryActivityParam);
        return AjaxResult.success(resultList);
    }

    /**
     * 导出
     * @param queryActivityParam
     */

    @PostMapping("export")
    public void export(QueryActivityParam queryActivityParam, HttpServletResponse response){
        QueryWrapper<AcBaseConfig> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(queryActivityParam.getActName())) {
            queryWrapper.eq("ACT_NAME", queryActivityParam.getActName());
        }
        if (queryActivityParam.getActivityStatus() != null) {
            // 状态筛选
            if (queryActivityParam.getActivityStatus() == EActivityStatus.NOT_STARTED.getValue()) {
                // 未开始  当前时间小于开始时间
                queryWrapper.lt("ACT_START_DATE", DateUtils.currentDate());
            } else if (queryActivityParam.getActivityStatus() == EActivityStatus.IN_PROGRESS.getValue()) {
                // 进行中 大于开始 小于结束
                queryWrapper.gt("ACT_START_DATE", DateUtils.currentDate());
                queryWrapper.lt("ACT_END_DATE", DateUtils.currentDate());
            } else if (queryActivityParam.getActivityStatus() == EActivityStatus.EXPIRED.getValue()) {
                // 已失效 当前时间大于结束时间
                queryWrapper.gt("ACT_END_DATE", DateUtils.currentDate());
            }
        }
        // 活动开始时间
        if (queryActivityParam.getStartActStartDate() != null && queryActivityParam.getEndActStartDate() != null) {
            queryWrapper.between("ACT_START_DATE", queryActivityParam.getStartActStartDate(), queryActivityParam.getEndActStartDate());
        }
        // 活动结束时间
        if (queryActivityParam.getStartActEndDate() != null && queryActivityParam.getEndActEndDate() != null) {
            queryWrapper.between("ACT_END_DATE", queryActivityParam.getStartActEndDate(), queryActivityParam.getEndActEndDate());
        }
        // 创建时间
        if (queryActivityParam.getStartCreationTime() != null && queryActivityParam.getEndCreationTime() != null) {
            queryWrapper.between("CREATION_TIME", queryActivityParam.getStartCreationTime(), queryActivityParam.getEndCreationTime());
        }
        queryWrapper.eq("ACT_TYPE",queryActivityParam.getActType());
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        // 创建时间倒叙
        queryWrapper.orderByDesc("CREATION_TIME");
        List<BaseConfigVo> resultList = new ArrayList<>();
        List<AcBaseConfig> list = baseConfigService.list(queryWrapper);
        for (AcBaseConfig acBaseConfig : list) {
            BaseConfigVo baseConfigVo = BeanUtil.copyProperties(acBaseConfig, BaseConfigVo.class);
            // 判断活动状态
            // 开始时间
            long startTime = acBaseConfig.getActStartDate().getTime();
            // 结束时间
            long endTime = acBaseConfig.getActEndDate().getTime();
            // 当前时间
            long currentTime = DateUtils.currentDate().getTime();
            if ((currentTime > startTime && currentTime < endTime) || currentTime == startTime || currentTime == endTime) {
                // 当前时间大于开始 小于结束
                baseConfigVo.setStatus(EActivityStatus.IN_PROGRESS.getLabel());
            } else if (currentTime < startTime) {
                // 当前时间小于开始时间
                baseConfigVo.setStatus(EActivityStatus.NOT_STARTED.getLabel());
            } else if (currentTime > endTime) {
                // 当前时间大于结束时间
                baseConfigVo.setStatus(EActivityStatus.EXPIRED.getLabel());
            }
            resultList.add(baseConfigVo);
        }
        ExcelUtil<BaseConfigVo> util = new ExcelUtil<>(BaseConfigVo.class);
        util.exportExcel(response, resultList, "活动配置导出");
    }



}
