package com.yanzhu.jh.project.controller;

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

import com.yanzhu.jh.project.domain.SurProject;
import com.yanzhu.jh.project.domain.SurProjectCostOutput;
import com.yanzhu.jh.project.service.ISurProjectCostOutputService;
import com.yanzhu.jh.project.service.ISurProjectService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目成本产值Controller
 * 
 * @author ruoyi
 * @date 2023-08-27
 */
@RestController
@RequestMapping("/project/costOutput")
public class SurProjectCostOutputController extends BaseController
{
    @Autowired
    private ISurProjectCostOutputService surProjectCostOutputService;

    @Autowired
    private ISurProjectService projectService;

    /**
     * 查询项目成本产值列表
     */
    @PreAuthorize("@ss.hasPermi('project:costOutput:list')")
    @GetMapping("/list")
    public TableDataInfo list(SurProjectCostOutput surProjectCostOutput)
    {
        startPage();
        List<SurProjectCostOutput> list = surProjectCostOutputService.selectSurProjectCostOutputList(surProjectCostOutput);
        return getDataTable(list);
    }

    /**
     * 导出项目成本产值列表
     */
    @PreAuthorize("@ss.hasPermi('project:costOutput:export')")
    @Log(title = "项目成本产值", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SurProjectCostOutput surProjectCostOutput)
    {
        List<SurProjectCostOutput> list = surProjectCostOutputService.selectSurProjectCostOutputList(surProjectCostOutput);
        ExcelUtil<SurProjectCostOutput> util = new ExcelUtil<SurProjectCostOutput>(SurProjectCostOutput.class);
        util.exportExcel(response, list, "项目成本产值数据");
    }

    /**
     * 获取项目成本产值详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:costOutput:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(surProjectCostOutputService.selectSurProjectCostOutputById(id));
    }

    /**
     * 新增项目成本产值
     */
    @PreAuthorize("@ss.hasPermi('project:costOutput:add')")
    @Log(title = "项目成本产值", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SurProjectCostOutput surProjectCostOutput)
    {
        return toAjax(surProjectCostOutputService.insertSurProjectCostOutput(surProjectCostOutput));
    }

    /**
     * 修改项目成本产值
     */
    @PreAuthorize("@ss.hasPermi('project:costOutput:edit')")
    @Log(title = "项目成本产值", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SurProjectCostOutput surProjectCostOutput)
    {
        return toAjax(surProjectCostOutputService.updateSurProjectCostOutput(surProjectCostOutput));
    }

    /**
     * 删除项目成本产值
     */
    @PreAuthorize("@ss.hasPermi('project:costOutput:remove')")
    @Log(title = "项目成本产值", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(surProjectCostOutputService.deleteSurProjectCostOutputByIds(ids));
    }

    /**
     * 按项目ID查询，如果没有数据进行初始化
     * @param projectId
     * @return
     */
    @GetMapping("/listByProjectId")
    public AjaxResult listByProjectId(long projectId){
        SurProjectCostOutput where =new SurProjectCostOutput();
        where.setProjectId(projectId);
        List<SurProjectCostOutput> list=surProjectCostOutputService.selectSurProjectCostOutputList(where);
        if(list.size()==0){
            surProjectCostOutputService.initProject(projectId);
        }
        list=surProjectCostOutputService.selectSurProjectCostOutputList(where);
        return AjaxResult.success(list);
    }

    /**
     * 增加年度投资
     * @param projectId
     * @param year
     * @return
     */
    @PostMapping("/addYearInvestment")
    public AjaxResult addYearInvestment(@RequestBody SurProjectCostOutput where){
        List<SurProjectCostOutput> list=surProjectCostOutputService.selectSurProjectCostOutputList(where);
        if(list.size()==0){
            surProjectCostOutputService.addYearInvestment(where);
            return AjaxResult.success("增加年度投资成功!");
        }else{
            return AjaxResult.error("此年度投资已存在!");
        }
    }
    @PostMapping("/addMonthInvestment")
    public AjaxResult addMonthInvestment(@RequestBody SurProjectCostOutput where) {
        List<SurProjectCostOutput> list=surProjectCostOutputService.selectSurProjectCostOutputList(where);
        if(list.size()==0){
            surProjectCostOutputService.addMonthInvestment(where);
            return AjaxResult.success("增加月度投资成功!");
        }else{
            return AjaxResult.error("此月度投资已存在!");
        }
    }


    @PostMapping("/batchUpdate")
    public AjaxResult batchUpdate(@RequestBody List<SurProjectCostOutput> list){
        try {
            for (SurProjectCostOutput it : list) {
                surProjectCostOutputService.updateSurProjectCostOutput(it);
            }
            return AjaxResult.success("保存成功!");
        }catch (Exception ex){
            return  AjaxResult.error(ex.getMessage());
        }
    }


    public AjaxResult test(){
        List<SurProject> list=projectService.selectSurProjectList(new SurProject());
        long y=2023l;
        long m=9l;
        SurProjectCostOutput where=new SurProjectCostOutput();
        where.setYear(y);
        where.setMonth(m);
        for(SurProject prj :list){
            where.setProjectId(prj.getId());
            System.out.println("---->"+prj.getId());
            List<SurProjectCostOutput> list2=surProjectCostOutputService.selectYearAndMonth(where);
            for(SurProjectCostOutput it :list2){
                if(it.getCostType().equals(1l)){
                    it.setMoney(toLong(prj.getTotalInvestment()));
                    surProjectCostOutputService.updateSurProjectCostOutput(it);
                }
                if(it.getCostType().equals(3l)){
                    it.setMoney(toLong(prj.getContractAmount()));
                    surProjectCostOutputService.updateSurProjectCostOutput(it);
                }
                if(it.getCostType().equals(4l)){
                    it.setMoney(toLong(prj.getPaidAmount()));
                    surProjectCostOutputService.updateSurProjectCostOutput(it);
                }
                if(it.getCostType().equals(5l)){
                    it.setMoney(toLong(prj.getOnAccountAmount()));
                    surProjectCostOutputService.updateSurProjectCostOutput(it);
                }
            }
            //cost_type 1 总投资 -总投资 totalInvestment
            //3-合同总额 contractAmount
            //4-付款金额 paidAmount
            //5-挂账金额 onAccountAmount
        }
        return AjaxResult.success();
    }

    private Long toLong(String totalInvestment) {
        try {
            return (long)(Double.parseDouble(totalInvestment)*10000);
        }catch (Exception ex){
            return 0l;
        }
    }

}
