package com.dsk.cscec.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.dsk.common.annotation.Log;
import com.dsk.common.constant.GlobalConstants;
import com.dsk.common.core.domain.PageQuery;
import com.dsk.common.core.domain.R;
import com.dsk.common.core.domain.entity.SysDictData;
import com.dsk.common.core.page.TableDataInfo;
import com.dsk.common.enums.BusinessType;
import com.dsk.common.excel.ExcelResult;
import com.dsk.common.helper.LoginHelper;
import com.dsk.common.utils.poi.ExcelUtil;
import com.dsk.common.utils.redis.RedisUtils;
import com.dsk.cscec.domain.CbCostMeasure;
import com.dsk.cscec.domain.bo.CbCostMeasureActualBo;
import com.dsk.cscec.domain.bo.CbCostMeasureActualPushBo;
import com.dsk.cscec.domain.bo.CbCostMeasureActualSaveBo;
import com.dsk.cscec.domain.vo.CbCostMeasureActualVo;
import com.dsk.cscec.domain.vo.CbCostMeasuresImportVo;
import com.dsk.cscec.domain.vo.CbCostMeasuresItemVo;
import com.dsk.cscec.listener.ProjectCostMeasureImportListener;
import com.dsk.cscec.service.ICbCostMeasureService;
import com.dsk.system.domain.vo.SysUserImportVo;
import com.dsk.system.listener.SysUserImportListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 措施费导入
 *
 * @author tanyang
 * @create 2024-02-04 11:44
 **/
@RequestMapping("/cb/cost/measures")
@RestController
public class CbCostMeasureController {

    @Autowired
    private ICbCostMeasureService cbCostMeasureService;


    /**
     * 措施费一级大类
     * 根据项目查询措施费一级大类
     *
     * @param projectId 项目ID
     * @return {@link R}<{@link List}<{@link Map}<{@link String}, {@link Object}>>>
     */
    @SaIgnore
    @GetMapping(value = "/type/{projectId}")
    public R<List<Map<String, Object>>> dictType(@PathVariable Long projectId) {
        List<Map<String, Object>> data = cbCostMeasureService.listByLevel(projectId,0);
        if (ObjectUtil.isNull(data)) {
            data = new ArrayList<>();
        }
        return R.ok(data);
    }


    /**
     * 措施费列表
     * 查询措施费列表
     *
     * @param cbCostMeasure CB成本衡量标准
     * @return {@link R}<{@link List}<{@link CbCostMeasureActualVo}>>
     */
//    @SaCheckPermission("cb:costmeasures:list")
    @SaIgnore
    @GetMapping("/list")
    public R<List<CbCostMeasureActualVo>> list(CbCostMeasureActualBo cbCostMeasure) {
        List<CbCostMeasureActualVo> data= cbCostMeasureService.selectDataList(cbCostMeasure);
        if (ObjectUtil.isNull(data)) {
            data = new ArrayList<>();
        }
        return R.ok(data);
    }




    /**
     * 批量保存或修改每月措施费
     */
//    @SaCheckPermission("cb:costmeasures:actual:saveBatch")
    @SaIgnore
    @PostMapping("/saveBatch")
    public R saveBatch(@RequestBody List<CbCostMeasureActualSaveBo> boList) {
        Assert.notEmpty(boList,"措施费填写信息不能为空！");
        cbCostMeasureService.saveBatchCostMeasureActual(boList);
        return R.ok();
    }


    /**
     * 推送工程量
     * 措施费-推送每月工程量
     *
     * @param pushBo 推送BO
     * @return {@link R}<{@link List}<{@link CbCostMeasureActualVo}>>
     */
//    @SaCheckPermission("cb:costmeasures:actual:push")
    @SaIgnore
    @PostMapping("/push/project/volume")
    public R pushProjectVolume(@RequestBody CbCostMeasureActualPushBo pushBo) {
        Assert.notNull(pushBo,"推送工程量信息不能为空！");
        cbCostMeasureService.pushCostMeasureActual(pushBo);
        return R.ok();
    }




    /**
     * 获取批量措施费列表
     */
    @Log(title = "措施费导出", businessType = BusinessType.EXPORT)
//    @SaCheckPermission("system:user:export")
    @PostMapping("/export")
    public void exportProjectMeasures(HttpServletResponse response) {
        String BATCH_IMPORT_FAIL_USERS = GlobalConstants.GLOBAL_REDIS_KEY + "batch_import_fail_users";
        String key = BATCH_IMPORT_FAIL_USERS + LoginHelper.getUserId();
        ExcelUtil.exportExcel(RedisUtils.getCacheList(key), "措施费", CbCostMeasuresImportVo.class, response);
        RedisUtils.deleteObject(key);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param projectId     项目id
     */
//    @SaIgnore
    @Log(title = "措施费导入", businessType = BusinessType.IMPORT)
//    @SaCheckPermission("system:user:import")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, Long projectId) throws Exception {
        cbCostMeasureService.importExcelData(file,projectId);
        return R.ok();
    }



    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param projectId     项目id
     */
    @SaIgnore
    @Log(title = "措施费汇总导入", businessType = BusinessType.IMPORT)
//    @SaCheckPermission("system:user:import")
    @PostMapping(value = "/summary/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importSummaryData(@RequestPart("file") MultipartFile file, Long projectId) throws Exception {
        Integer dataType=2;
        cbCostMeasureService.importExcelSummaryData(file,projectId,dataType);
        return R.ok();
    }


    public static void main(String[] args) throws FileNotFoundException {
        List list =new ArrayList<>();

        File file=new File("E:\\dingding\\202401\\导入表格\\措施费\\大型机械费.xlsx");
        FileInputStream inputStream=new FileInputStream(file);


        ExcelResult<CbCostMeasuresImportVo> result = ExcelUtil.importExcel(inputStream, CbCostMeasuresImportVo.class, new ProjectCostMeasureImportListener(1L,1));
        String analysis = result.getAnalysis();
        List<CbCostMeasuresImportVo> list1 = result.getList();


        System.out.println(">>>>>>>>>>"+analysis);
//        System.out.println(JSONUtil.toJsonStr(list));

    }


}
