package com.qs.mp.web.controller.api.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qs.mp.admin.domain.PrizeRecovery;
import com.qs.mp.admin.domain.excel.PrizeRecoveryExcel;
import com.qs.mp.admin.domain.param.PrizeRecoveryListQueryParam;
import com.qs.mp.admin.domain.vo.PrizeRecoveryListVO;
import com.qs.mp.admin.service.IPrizeRecoveryService;
import com.qs.mp.common.core.domain.AjaxResult;
import com.qs.mp.common.core.page.TableDataInfo;
import com.qs.mp.common.utils.DateUtils;
import com.qs.mp.common.utils.StringUtils;
import com.qs.mp.user.domain.excel.DeliverOrderExcel;
import com.qs.mp.user.domain.vo.UserDeliverOrderItemVO;
import com.qs.mp.user.domain.vo.UserDeliverOrderVO;
import com.qs.mp.utils.ExcelUtil;
import com.qs.mp.web.controller.common.BaseApiController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 用户中奖商品回收相关接口
 * @author Cup
 * @date 2022/5/25
 */
@RestController
@RequestMapping("/api/v1/mp/admin/prize/recovery")
@Api(tags = "用户奖品回收相关接口")
public class UserPrizeRecoveryMgrController extends BaseApiController {

    @Autowired
    private IPrizeRecoveryService prizeRecoveryService;


    @PostMapping("/list")
    @PreAuthorize("@ss.hasPermi('business:recovery:list')")
    @ApiOperation("回收订单列表,支持分页")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = PrizeRecoveryListVO.class)
    })
    public TableDataInfo list(@RequestBody PrizeRecoveryListQueryParam prizeRecoveryListQueryParam) {
        startPage();

        QueryWrapper<PrizeRecovery> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(prizeRecoveryListQueryParam.getTitle())) {
            queryWrapper.like("t1.title",prizeRecoveryListQueryParam.getTitle());
        }
        if (StringUtils.isNotBlank(prizeRecoveryListQueryParam.getUserPhone())) {
            queryWrapper.eq("t2.phonenumber", prizeRecoveryListQueryParam.getUserPhone());
        }
        if (Objects.nonNull(prizeRecoveryListQueryParam.getStartTime()) && Objects.nonNull(prizeRecoveryListQueryParam.getEndTime())) {
            queryWrapper.between("t1.created_time", prizeRecoveryListQueryParam.getStartTime(), prizeRecoveryListQueryParam.getEndTime());
        }
        queryWrapper.orderByDesc("t1.created_time");
        List<PrizeRecoveryListVO> list =  prizeRecoveryService.listByWrapper(queryWrapper);

        return getDataTable(list);
    }

    @PostMapping("/export")
    @PreAuthorize("@ss.hasPermi('business:recovery:export')")
    @ApiOperation("回收订单列表导出")
    public AjaxResult export(@RequestBody PrizeRecoveryListQueryParam prizeRecoveryListQueryParam) {

        // 导出时间范围限制校验
        Date startTime = prizeRecoveryListQueryParam.getStartTime();
        Date endTime = prizeRecoveryListQueryParam.getEndTime();
        if (startTime == null || endTime == null) {
            return AjaxResult.error("导出数据必须设置日期范围");
        }
        if (DateUtils.diff(startTime, endTime) > 30) {
            return AjaxResult.error("导出的数据不能超过31天");
        }

        QueryWrapper<PrizeRecovery> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(prizeRecoveryListQueryParam.getTitle())) {
            queryWrapper.like("t1.title",prizeRecoveryListQueryParam.getTitle());
        }
        if (StringUtils.isNotBlank(prizeRecoveryListQueryParam.getUserPhone())) {
            queryWrapper.eq("t2.phonenumber", prizeRecoveryListQueryParam.getUserPhone());
        }
        if (Objects.nonNull(prizeRecoveryListQueryParam.getStartTime()) && Objects.nonNull(prizeRecoveryListQueryParam.getEndTime())) {
            queryWrapper.between("t1.created_time", prizeRecoveryListQueryParam.getStartTime(), prizeRecoveryListQueryParam.getEndTime());
        }
        queryWrapper.orderByDesc("t1.created_time");
        int totalSize =  prizeRecoveryService.countByWrapper(queryWrapper);
        if (totalSize == 0) {
            return AjaxResult.error("没有符合条件的用户提货订单");
        }

        List<PrizeRecoveryExcel> excelList = new ArrayList<>();
        List<PrizeRecoveryListVO> listAll = new ArrayList<>();

        int pageSize = 2000;
        if (totalSize > pageSize) {
            int totalPage = totalSize % pageSize == 0 ? totalSize / pageSize : totalSize / pageSize + 1;
            for (int i = 0; i < totalPage; i++) {
                queryWrapper.last("limit " + (i * pageSize) + ", " + pageSize);
                List<PrizeRecoveryListVO> prizeRecoveryListVOS = prizeRecoveryService.listByWrapper(queryWrapper);
                if (null != prizeRecoveryListVOS && prizeRecoveryListVOS.size() > 0) {
                    listAll.addAll(prizeRecoveryListVOS);
                }
            }
        } else {
            listAll = prizeRecoveryService.listByWrapper(queryWrapper);
        }
        for (PrizeRecoveryListVO prizeRecoveryListVO : listAll) {
            PrizeRecoveryExcel prizeRecoveryExcel = new PrizeRecoveryExcel();
            BeanUtils.copyProperties(prizeRecoveryListVO, prizeRecoveryExcel);
            excelList.add(prizeRecoveryExcel);
        }

        ExcelUtil<PrizeRecoveryExcel> util = new ExcelUtil<>(PrizeRecoveryExcel.class);
        return util.exportExcel(excelList, "商品回收订单导出", false);
    }

}
