package com.zzsmart.qomo.kn.cost.manage.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzsmart.qomo.kn.cost.manage.entity.CostStandardDetail;
import com.zzsmart.qomo.kn.cost.manage.mapper.CostStandardDetailMapper;
import com.zzsmart.qomo.kn.cost.manage.service.CostStandardDetailService;
import com.zzsmart.qomo.kn.cost.manage.vo.CostStandardDetailSearch;
import com.zzsmart.qomo.kn.cost.manage.vo.CostStandardDetailVO;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Set;

import static com.zzsmart.qomo.kn.cost.manage.util.EasyExcelUtil.getIncludeFields;

/**
 * <p>
 * 标准成本明细表 服务实现类
 * </p>
 *
 * @author app_scene_cost_purchase_price
 * @since 2024-06-19
 */
@Service
public class CostStandardDetailServiceImpl extends ServiceImpl<CostStandardDetailMapper, CostStandardDetail> implements CostStandardDetailService {

    @Autowired
    CostStandardDetailMapper costStandardDetailMapper;

    /**
     * 分页查询
     *
     * @param page
     * @param costStandard
     * @return
     */
    @Override
    public IPage<CostStandardDetailVO> pageList(Page<CostStandardDetailVO> page, CostStandardDetailVO costStandard) {
        // 查询条件组装
        QueryWrapper queryWrapper = new QueryWrapper();

        if (costStandard != null) {

            if (StrUtil.isNotEmpty(costStandard.getMaterialNumber())) {
                // 物料号
                queryWrapper.like("c1.material_number", costStandard.getMaterialNumber());
            }
            if (StrUtil.isNotEmpty(costStandard.getVersionNumber())) {
                // 版本号
                queryWrapper.like("c2.version_number", costStandard.getVersionNumber());
            }

        }
        return (IPage<CostStandardDetailVO>) costStandardDetailMapper.pageList(page, queryWrapper);
    }

    @Override
    public JSONObject exportExcel(HttpServletRequest request, HttpServletResponse response, CostStandardDetailSearch costStandardDetailSearch) {
        QueryWrapper<CostStandardDetail> queryWrapper = QueryGenerator.initQueryWrapper(costStandardDetailSearch, request.getParameterMap());
        if (costStandardDetailSearch != null && costStandardDetailSearch.getSelections() != null && costStandardDetailSearch.getSelections().size() > 0) {
            queryWrapper.in("id", costStandardDetailSearch.getSelections());
        }
        List exportList = list(queryWrapper);
//        EasyExcelUtil.export(response, "D:/WorkDoc/PHM/单机部署/单行查询.xls", null, exportList,CostStandardDetail.class);
        // 根据用户传入字段 假设我们要忽略 date
        Set<String> includeFields = getIncludeFields(CostStandardDetail.class);
        EasyExcel.write("D:/WorkDoc/PHM/单机部署/单行查询.xls", CostStandardDetail.class).includeColumnFieldNames(includeFields).sheet("模板").doWrite(exportList);
        // return super.exportXls(request, costStandardDetail, CostStandardDetail.class, "单行查询");
        JSONObject result = new JSONObject();
        result.put("filePath", "D:/WorkDoc/PHM/单机部署/单行查询.xls");
        return result;
    }

}
