package com.dsk.cscec.controller;


import cn.hutool.core.bean.BeanUtil;
import com.dsk.common.core.controller.BaseController;
import com.dsk.common.core.domain.R;
import com.dsk.common.utils.poi.ExcelUtil;
import com.dsk.cscec.domain.bo.CbSummaryActualBo;
import com.dsk.cscec.domain.bo.CbSummaryActualListBo;
import com.dsk.cscec.domain.bo.CbSummaryActualLockBo;
import com.dsk.cscec.domain.bo.CbSummaryBo;
import com.dsk.cscec.domain.vo.CbSummaryActualListVo;
import com.dsk.cscec.domain.vo.CbSummaryCostAccountExportVo;
import com.dsk.cscec.domain.vo.CbSummaryProjectExportVo;
import com.dsk.cscec.service.CbSummaryService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 成本汇总(CbSummary)表控制层
 *
 * @author cyf
 * @since 2024-02-06
 */
@RestController
@RequestMapping("/cbSummary")
public class CbSummaryController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private CbSummaryService cbSummaryService;


    /**
     * 成本汇总左侧一级数据列表
     *
     * @param bo
     * @return
     */
    @GetMapping("/cbNameList")
    public R<List<Map<String, Object>>> getCbNameList(CbSummaryBo bo) {
        return R.ok(cbSummaryService.getCbNameList(bo));
    }

    /**
     * 成本汇总数据列表
     * @param bo
     * @return
     */
    @GetMapping("/list")
    public R<List<CbSummaryActualListVo>> getList(CbSummaryActualListBo bo) {
        return R.ok(cbSummaryService.getActualList(bo));
    }

    /**
     * 已添加成本月份
     *
     * @param bo
     * @return
     */
    @GetMapping("/expenseDateList")
    public R<List<Map<String,Object>>> getExpenseDateList(@Validated CbSummaryBo bo) {
        return R.ok(cbSummaryService.getExpenseDateList(bo));
    }

    /**
     * 已锁定月份
     * @param bo
     * @return
     */
    @GetMapping("/expenseDateList/locked")
    public R<List<String>> getLockedList(@Validated CbSummaryBo bo) {
        return R.ok(cbSummaryService.getLockedList(bo));
    }

    /**
     * 编辑成本
     *
     * @param boList
     * @return
     */
    @PutMapping("/editActual")
    public R editActual(@RequestBody List<CbSummaryActualBo> boList) {
        return cbSummaryService.insertOrUpdateActual(boList) == true ? R.ok() : R.fail();
    }

    /**
     * 锁定成本前提示未填项（按一级大类）
     * @param bo
     * @return
     */
    @GetMapping("/getUnfilled")
    public R getUnfilled(CbSummaryActualLockBo bo){
        return cbSummaryService.getUnfilled(bo);
    }

    /**
     * 按月份锁定成本
     * @param bo
     * @return
     */
    @PostMapping("/lockActual")
    public R lockActual(@RequestBody CbSummaryActualLockBo bo) {
        return cbSummaryService.lockActual(bo) == true ? R.ok() : R.fail();
    }

    /**
     * 导出excel-按月导出所有
     * @param bo
     * @param response
     */
    @PostMapping("/export")
    public void export(@RequestBody CbSummaryActualListBo bo, HttpServletResponse response) {
        List<CbSummaryActualListVo> actualListVoList = cbSummaryService.getAll(bo);
        if(bo.getCbType()==1){
            List<CbSummaryProjectExportVo> list = BeanUtil.copyToList(actualListVoList,CbSummaryProjectExportVo.class);
            ExcelUtil.exportExcel(list, "成本汇总按项目汇总"+bo.getExpenseDate(), CbSummaryProjectExportVo.class, response);
        }else {
            List<CbSummaryCostAccountExportVo> list = BeanUtil.copyToList(actualListVoList,CbSummaryCostAccountExportVo.class);
            ExcelUtil.exportExcel(list, "成本汇总按成本科目汇总"+bo.getExpenseDate(), CbSummaryCostAccountExportVo.class, response);
        }

    }

    /**
     * 解析并保存数据
     * @param projectId
     */
    @PostMapping("/analysisData")
    public void analysisData(Long projectId){
        cbSummaryService.importCbProject(projectId);
    }

}
