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

import cn.hutool.core.bean.BeanUtil;
import com.hzs.bonus.bonus.param.CuMemberBonusDelayParam;
import com.hzs.bonus.bonus.service.ICuMemberBonusDelayService;
import com.hzs.bonus.bonus.vo.BonusDelayVO;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
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.page.TableDataInfo;
import com.hzs.common.domain.member.bonus.CuMemberBonusDelay;
import com.hzs.common.domain.member.bonus.ext.CuMemberBonusDelayExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 会员奖金-拓展收益延缓表 前端控制器
 */
@RestController
@RequestMapping("/manage/bonus-delay")
public class CuMemberBonusDelayController extends BaseController {

    @Autowired
    private ICuMemberBonusDelayService iCuMemberBonusDelayService;

    /**
     * 拓展延缓列表
     *
     * @return
     */
    @GetMapping("/delay-list")
    public TableDataInfo delayList(CuMemberBonusDelayParam param) {
        if (param.getStartDate() != null) {
            param.setStartDate(DateUtils.getStartTime(param.getStartDate()));
        }
        if (param.getEndDate() != null) {
            param.setEndDate(DateUtils.getEndTime(param.getEndDate()));
        }

        List<CuMemberBonusDelayExt> resultList = iCuMemberBonusDelayService.listDelay(param);
        return getDataTable(resultList);
    }

    /**
     * 导出拓展延缓列表
     **/
    @Log(module = EOperationModule.BONUS_DELAY, business = EOperationBusiness.BONUS_DELAY, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CuMemberBonusDelayParam param) {
        if (param.getStartDate() != null) {
            param.setStartDate(DateUtils.getStartTime(param.getStartDate()));
        }
        if (param.getEndDate() != null) {
            param.setEndDate(DateUtils.getEndTime(param.getEndDate()));
        }

        List<BonusDelayVO> resultList = new ArrayList<>();
        List<CuMemberBonusDelayExt> list = iCuMemberBonusDelayService.listDelay(param);

        if (StringUtils.isNotEmpty(list)) {
            resultList = BeanUtil.copyToList(list, BonusDelayVO.class);
        }
        ExcelUtil<BonusDelayVO> util = new ExcelUtil<>(BonusDelayVO.class);
        util.exportExcel(response, resultList, "拓展延缓");
    }
}
