package com.hzs.sale.all.controller.manage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzs.common.core.enums.ESysName;
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.sale.all.AllProductDetail;
import com.hzs.sale.all.param.AllProductQueryParam;
import com.hzs.sale.all.service.IAllProductDetailService;
import com.hzs.sale.all.vo.AllProductDetailQueryVO;
import lombok.extern.slf4j.Slf4j;
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;

/**
 * 全网产品明细控制器
 */
@RestController
@RequestMapping("/manage/all-product-detail")
@Slf4j
public class AllProductDetailController extends BaseController {

    @Autowired
    private IAllProductDetailService iAllProductDetailService;

    /**
     * 产品明细列表
     */
    @GetMapping("/list")
    public TableDataInfo productList(AllProductQueryParam param) {
        List<AllProductDetailQueryVO> resultList = new ArrayList<>();

        startPage();
        LambdaQueryWrapper<AllProductDetail> queryWrapper = new LambdaQueryWrapper<>();
        if (null != param.getPkAllProduct()) {
            queryWrapper.eq(AllProductDetail::getPkAllProduct, param.getPkAllProduct());
        }
        if (StringUtils.isNotEmpty(param.getSource())) {
            queryWrapper.eq(AllProductDetail::getSource, param.getSource());
        }
        if (StringUtils.isNotEmpty(param.getOrderCode())) {
            queryWrapper.like(AllProductDetail::getOrderCode, param.getOrderCode());
        }
        if (StringUtils.isNotEmpty(param.getStartDate())) {
            queryWrapper.ge(AllProductDetail::getCreationTime, DateUtils.getStartTime(DateUtils.parseDateOne(param.getStartDate(), DateUtils.YYYY_MM_DD)));
        }
        if (StringUtils.isNotEmpty(param.getEndDate())) {
            queryWrapper.le(AllProductDetail::getCreationTime, DateUtils.getEndTime(DateUtils.parseDateOne(param.getEndDate(), DateUtils.YYYY_MM_DD)));
        }
        queryWrapper.orderByDesc(AllProductDetail::getCreationTime);
        List<AllProductDetail> productList = iAllProductDetailService.list(queryWrapper);

        if (CollectionUtil.isNotEmpty(productList)) {
            for (AllProductDetail allProductDetail : productList) {
                AllProductDetailQueryVO vo = BeanUtil.copyProperties(allProductDetail, AllProductDetailQueryVO.class);
                vo.setSourceVal(ESysName.getLabelByValue(vo.getSource()));
                resultList.add(vo);
            }
        }

        return getDataTable(resultList);
    }

    /**
     * 产品明细导出
     *
     * @param response
     * @param param
     */
    @PostMapping("/export")
    public void productListExport(HttpServletResponse response, AllProductQueryParam param) {
        List<AllProductDetailQueryVO> resultList = new ArrayList<>();
        LambdaQueryWrapper<AllProductDetail> queryWrapper = new LambdaQueryWrapper<>();
        if (null != param.getPkAllProduct()) {
            queryWrapper.eq(AllProductDetail::getPkAllProduct, param.getPkAllProduct());
        }
        if (StringUtils.isNotEmpty(param.getSource())) {
            queryWrapper.eq(AllProductDetail::getSource, param.getSource());
        }
        if (StringUtils.isNotEmpty(param.getOrderCode())) {
            queryWrapper.like(AllProductDetail::getOrderCode, param.getOrderCode());
        }
        if (StringUtils.isNotEmpty(param.getStartDate())) {
            queryWrapper.ge(AllProductDetail::getCreationTime, DateUtils.getStartTime(DateUtils.parseDateOne(param.getStartDate(), DateUtils.YYYY_MM_DD)));
        }
        if (StringUtils.isNotEmpty(param.getEndDate())) {
            queryWrapper.le(AllProductDetail::getCreationTime, DateUtils.getEndTime(DateUtils.parseDateOne(param.getEndDate(), DateUtils.YYYY_MM_DD)));
        }
        queryWrapper.orderByDesc(AllProductDetail::getCreationTime);
        List<AllProductDetail> productList = iAllProductDetailService.list(queryWrapper);

        if (CollectionUtil.isNotEmpty(productList)) {
            for (AllProductDetail allProductDetail : productList) {
                AllProductDetailQueryVO vo = BeanUtil.copyProperties(allProductDetail, AllProductDetailQueryVO.class);
                vo.setSourceVal(ESysName.getLabelByValue(vo.getSource()));
                resultList.add(vo);
            }
        }

        ExcelUtil<AllProductDetailQueryVO> util = new ExcelUtil<>(AllProductDetailQueryVO.class);
        util.exportExcel(response, resultList, "产品明细导出");
    }

}
