package com.dsk.cscec.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dsk.common.annotation.Log;
import com.dsk.common.core.domain.R;
import com.dsk.common.enums.BusinessType;
import com.dsk.cscec.domain.CbProjectExpenseSummary;
import com.dsk.cscec.domain.bo.CbProjectBaseBo;
import com.dsk.cscec.domain.bo.CbProjectOtherBo;
import com.dsk.cscec.domain.vo.CbProjectOtherVo;
import com.dsk.cscec.service.CbProjectExpenseSummaryService;
import com.dsk.cscec.service.CbProjectOtherService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CbProjectOtherController
 * @Description 其他费用相关控制器
 * @Author Dgm
 * @Date 2024/2/8 10:55
 * @Version 1.0.0
 */
@RestController
@RequestMapping("cb/projectOther")
public class CbProjectOtherController {


    @Resource
    private CbProjectOtherService projectOtherService;

    @Resource
    private CbProjectExpenseSummaryService cbProjectExpenseSummaryService;

    @GetMapping(value = "/projectOther/dataAnalysis")
    public R<List<CbProjectExpenseSummary>> projectOtherDataAnalysis(CbProjectBaseBo baseBo) throws Exception{
        projectOtherService.projectOtherDataAnalysis(baseBo);
        return R.ok();
    }

    /***
     * @Description: 其他费用-汇总
     * @Param:
     * @return: com.dsk.common.core.domain.R<java.util.List<com.dsk.cscec.domain.vo.CbProjectOtherVo>>
     * @Author: Dgm
     * @date: 2024/2/8 14:08
     */
    @SaIgnore
    @GetMapping(value = "/statistics/{projectId}")
    public R<List<CbProjectExpenseSummary>> statistics(@PathVariable Long projectId) {
        CbProjectExpenseSummary expenseSummary = new CbProjectExpenseSummary();
        expenseSummary.setProjectId(projectId);
        expenseSummary.setDataType(3);
        QueryWrapper<CbProjectExpenseSummary> queryWrapper = Wrappers.query(expenseSummary);
        queryWrapper.select("id","expense_name as expenseName","expense_value as expenseValue","proportion");
        queryWrapper.orderByAsc("id");
        List<CbProjectExpenseSummary> expenseSummaryList = this.cbProjectExpenseSummaryService.list(queryWrapper);
        if (ObjectUtil.isEmpty(expenseSummaryList)) {
            expenseSummaryList = new ArrayList<>();
        }
        return R.ok(expenseSummaryList);
    }

    /***
     * @Description: 其他费用一级大类
     * @Param:
     * @return: com.dsk.common.core.domain.R<java.util.List<com.dsk.cscec.domain.vo.CbProjectOtherVo>>
     * @Author: Dgm
     * @date: 2024/2/8 14:08
     */
    @SaIgnore
    @GetMapping(value = "/type/{projectId}")
    public R<List<Map<String, Object>>> dictType(@PathVariable Long projectId) {
        List<Map<String, Object>> data = projectOtherService.listByLevel(projectId,0);
        if (ObjectUtil.isNull(data)) {
            data = new ArrayList<>();
        }
        return R.ok(data);
    }

    /***
     * @Description: 其他费用列表
     * @Param:
     * @return: com.dsk.common.core.domain.R<java.util.List<com.dsk.cscec.domain.vo.CbProjectOtherVo>>
     * @Author: Dgm
     * @date: 2024/2/8 14:08
     */
    @SaIgnore
    @GetMapping("/list")
    public R<List<CbProjectOtherVo>> list(CbProjectOtherBo projectOtherBo) {
        List<CbProjectOtherVo> data= projectOtherService.selectDataList(projectOtherBo);
        if (ObjectUtil.isNull(data)) {
            data = new ArrayList<>();
        }
        return R.ok(data);
    }

    /***
     * @Description: 其他费用-导入数据
     * @param file          导入文件
     * @param projectId     项目id
     * @return: com.dsk.common.core.domain.R<java.util.List<com.dsk.cscec.domain.vo.CbProjectOtherVo>>
     * @Author: Dgm
     * @date: 2024/2/8 14:08
     */
    @Log(title = "其他费用相关项导入", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, Long projectId) throws Exception {
        projectOtherService.importExcelData(file,projectId);
        return R.ok();
    }
}
