package com.qianhe.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qianhe.common.core.domain.entity.SysUser;
import com.qianhe.common.exception.BusinessException;
import com.qianhe.common.utils.DateUtils;
import com.qianhe.common.utils.SecurityUtils;
import com.qianhe.common.utils.StringUtils;
import com.qianhe.domain.SjFybp;
import com.qianhe.domain.SjGfjsZsdwkhpj;
import com.qianhe.system.domain.SysDeptCb;
import com.qianhe.system.mapper.SysDeptCbMapper;
import com.qianhe.system.mapper.SysDeptMapper;
import com.qianhe.util.LogUtil;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.qianhe.common.annotation.Log;
import com.qianhe.common.core.controller.BaseController;
import com.qianhe.common.core.domain.AjaxResult;
import com.qianhe.common.enums.BusinessType;
import com.qianhe.domain.SjFybpZb;
import com.qianhe.service.ISjFybpZbService;
import com.qianhe.common.utils.poi.ExcelUtil;
import com.qianhe.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 三基-费用报批主Controller
 *
 * @author qianhe
 * @date 2024-07-11
 */
@RestController
@RequestMapping("/system/sjFybpZb")
public class SjFybpZbController extends BaseController
{
    @Autowired
    private ISjFybpZbService sjFybpZbService;

    @Autowired
    private SysDeptCbMapper sysDeptCbMapper;
    /**
     * 查询三基-费用报批主列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SjFybpZb sjFybpZb)
    {

        //日志
        SysUser u = SecurityUtils.getLoginUser().getUser();
        LogUtil.mkrzsc(u, "费用报批", "费用报批-年度计划", "查询年度计划上报", "查询");

        startPage();
        List<SjFybpZb> list = sjFybpZbService.selectSjFybpZbList(sjFybpZb);
        return getDataTable(list);
    }

    /**
     * 导出三基-费用报批主列表
     */
    @Log(title = "三基-费用报批主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SjFybpZb sjFybpZb)
    {
        List<SjFybpZb> list = sjFybpZbService.selectSjFybpZbList(sjFybpZb);
        ExcelUtil<SjFybpZb> util = new ExcelUtil<SjFybpZb>(SjFybpZb.class);
        util.exportExcel(response, list, "三基-费用报批主数据");
    }

    /**
     * 获取三基-费用报批主详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {

        //日志
        SysUser u = SecurityUtils.getLoginUser().getUser();
        LogUtil.mkrzsc(u, "费用报批", "费用报批-年度计划", "查询年度计划上报", "查询");

        return success(sjFybpZbService.selectSjFybpZbById(id));
    }

    /**
     * 新增三基-费用报批主
     */
    @Log(title = "三基-费用报批主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SjFybpZb sjFybpZb)
    {

        //日志
        SysUser u = SecurityUtils.getLoginUser().getUser();
        LogUtil.mkrzsc(u, "费用报批", "费用报批-年度计划", "新增年度计划上报", "新增");

        int i = sjFybpZbService.insertSjFybpZb(sjFybpZb);
        SysDeptCb sysDeptCb =new SysDeptCb();
        sysDeptCb.setNd(sjFybpZb.getFyrq());
        sysDeptCb.setDeptId(sjFybpZb.getDeptId());
        SysDeptCb deptCb = sysDeptCbMapper.getInfoBydeptId(sysDeptCb);
        String msg="";
        if(deptCb==null){
            msg="保存成功！您单位基础信息未维护，请到部门管理中进行设置！";
        }else {
           if(StringUtils.isNotEmpty( deptCb.getDwlx() )){
               String dwlx = deptCb.getDwlx();
               BigDecimal jyqlhjysr = deptCb.getJyqlhjysr();//万吨 换算成吨
               BigDecimal jcrysl = BigDecimal.valueOf(deptCb.getJcrysl());
               BigDecimal jcdwsl = BigDecimal.valueOf(deptCb.getJcdwsl());
               BigDecimal yxbzsl = BigDecimal.valueOf(deptCb.getYxbzsl());
               BigDecimal fyje = sjFybpZb.getFyje()!=null?sjFybpZb.getFyje():new BigDecimal(0);
               fyje=fyje.setScale(2,BigDecimal.ROUND_HALF_UP);
               if("油气生产单位".equals(dwlx)){
                   //计算标准
                   jyqlhjysr= deptCb.getJyqlhjysr().multiply(new BigDecimal(10000));//万吨 换算成吨
                   BigDecimal jys= jyqlhjysr.multiply(new BigDecimal(2)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                   BigDecimal rys=jcrysl.multiply(new BigDecimal(1000)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                   BigDecimal dws=jcdwsl.multiply(new BigDecimal(150000)).multiply(new BigDecimal(0.2)).setScale(2,BigDecimal.ROUND_HALF_UP);
                   BigDecimal bzs=yxbzsl.multiply(new BigDecimal(20000)).multiply(new BigDecimal(0.2)).setScale(2,BigDecimal.ROUND_HALF_UP);
                   BigDecimal sum=jys.add(rys).add(dws).add(bzs);
                   sum=sum.divide(new BigDecimal(10000)).setScale(2,BigDecimal.ROUND_HALF_UP);
                   if(fyje.compareTo(sum)>-1){
                     msg="保存成功！费用总额"+fyje+"万元不低于"+sum+"万元。\n计算标准为："
                             +deptCb.getJyqlhjysr().setScale(2,BigDecimal.ROUND_HALF_UP)+"万吨*2元/吨*30%+"+jcrysl+"*1000元/人*30%+"+jcdwsl+"*15万元/单位*20%+"+yxbzsl+"*2万元/班站*20%="+sum;
                   }else {
                       msg="保存成功！费用总额"+fyje+"万元低于"+sum+"万元。\n计算标准为："
                               +deptCb.getJyqlhjysr().setScale(2,BigDecimal.ROUND_HALF_UP)+"万吨*2元/吨*30%+"+jcrysl+"*1000元/人*30%+"+jcdwsl+"*15万元/单位*20%+"+yxbzsl+"*2万元/班站*20%="+sum;
                   }
               }else if("专业化单位".equals(dwlx)) {
                   //计算标准
                   if(yxbzsl.equals(new BigDecimal(0))){
                       BigDecimal jys= jyqlhjysr.multiply(new BigDecimal(0.0001)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                       BigDecimal rys=jcrysl.multiply(new BigDecimal(1000)).multiply(new BigDecimal(0.4)).setScale(2,BigDecimal.ROUND_HALF_UP);
                       BigDecimal dws=jcdwsl.multiply(new BigDecimal(150000)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                       BigDecimal sum=jys.add(rys).add(dws);
                       sum=sum.divide(new BigDecimal(10000)).setScale(2,BigDecimal.ROUND_HALF_UP);
                       if(fyje.compareTo(sum)>-1){
                           msg="保存成功！费用总额"+fyje+"万元不低于"+sum+"万元。\n计算标准为："
                                   +deptCb.getJyqlhjysr().setScale(2,BigDecimal.ROUND_HALF_UP)+"万元*1‰*30%+"+jcrysl+"*1000元/人*40%+"+jcdwsl+"*15万元/单位*30%="+sum;
                       }else {
                           msg="保存成功！费用总额"+fyje+"万元低于"+sum+"万元。\n计算标准为："
                                   +deptCb.getJyqlhjysr().setScale(2,BigDecimal.ROUND_HALF_UP)+"万吨*2元/吨*30%+"+jcrysl+"*1000元/人*40%+"+jcdwsl+"*15万元/单位*30%="+sum;
                       }
                   }else {
                       BigDecimal jys= jyqlhjysr.multiply(new BigDecimal(0.0001)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                       BigDecimal rys=jcrysl.multiply(new BigDecimal(1000)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                       BigDecimal dws=jcdwsl.multiply(new BigDecimal(150000)).multiply(new BigDecimal(0.2)).setScale(2,BigDecimal.ROUND_HALF_UP);
                       BigDecimal bzs=yxbzsl.multiply(new BigDecimal(20000)).multiply(new BigDecimal(0.2)).setScale(2,BigDecimal.ROUND_HALF_UP);
                       BigDecimal sum=jys.add(rys).add(dws).add(bzs);
                       sum=sum.divide(new BigDecimal(10000)).setScale(2,BigDecimal.ROUND_HALF_UP);
                       if(fyje.compareTo(sum)>-1){
                           msg="保存成功！费用总额"+fyje+"万元不低于"+sum+"万元。\n计算标准为："
                                   +deptCb.getJyqlhjysr().setScale(2,BigDecimal.ROUND_HALF_UP)+"万元*1‰*30%+"+jcrysl+"*1000元/人*30%+"+jcdwsl+"*15万元/单位*20%+"+yxbzsl+"*2万元/班站*20%="+sum;
                       }else {
                           msg="保存成功！费用总额"+fyje+"万元低于"+sum+"万元。\n计算标准为："
                                   +deptCb.getJyqlhjysr().setScale(2,BigDecimal.ROUND_HALF_UP)+"万吨*2元/吨*30%+"+jcrysl+"*1000元/人*30%+"+jcdwsl+"*15万元/单位*20%+"+yxbzsl+"*2万元/班站*20%="+sum;
                       }

                   }
               }else if("科研监督单位".equals(dwlx)) {
                   BigDecimal jys= jyqlhjysr.multiply(new BigDecimal(0.0001)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                   BigDecimal rys=jcrysl.multiply(new BigDecimal(1000)).multiply(new BigDecimal(0.4)).setScale(2,BigDecimal.ROUND_HALF_UP);
                   BigDecimal dws=jcdwsl.multiply(new BigDecimal(50000)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                   BigDecimal sum=jys.add(rys).add(dws);
                   sum=sum.divide(new BigDecimal(10000)).setScale(2,BigDecimal.ROUND_HALF_UP);
                   if(fyje.compareTo(sum)>-1){
                       msg="保存成功！费用总额"+fyje+"万元不低于"+sum+"万元。\n计算标准为："
                               +deptCb.getJyqlhjysr().setScale(2,BigDecimal.ROUND_HALF_UP)+"万元*1‰*30%+"+jcrysl+"*1000元/人*40%+"+jcdwsl+"*5万元/单位*30%="+sum;
                   }else {
                       msg="保存成功！费用总额"+fyje+"万元低于"+sum+"万元。\n计算标准为："
                               +deptCb.getJyqlhjysr().setScale(2,BigDecimal.ROUND_HALF_UP)+"万吨*2元/吨*30%+"+jcrysl+"*1000元/人*40%+"+jcdwsl+"*5万元/单位*30%="+sum;
                   }
               }
           }
        }
        return AjaxResult.success(msg);
    }

    /**
     * 修改三基-费用报批主
     */

    @Log(title = "三基-费用报批主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SjFybpZb sjFybpZb)
    {

        //日志
        SysUser u = SecurityUtils.getLoginUser().getUser();
        LogUtil.mkrzsc(u, "费用报批", "费用报批-年度计划", "修改年度计划上报", "修改");

        int i = sjFybpZbService.updateSjFybpZb(sjFybpZb);
        SysDeptCb sysDeptCb =new SysDeptCb();
        sysDeptCb.setNd(sjFybpZb.getFyrq());
        sysDeptCb.setDeptId(sjFybpZb.getDeptId());
        SysDeptCb deptCb = sysDeptCbMapper.getInfoBydeptId(sysDeptCb);
        String msg="";
        if(deptCb==null){
            msg="保存成功！您单位基础信息未维护，请到部门管理中进行设置！";
        }else {
            if(StringUtils.isNotEmpty( deptCb.getDwlx() )){
                String dwlx = deptCb.getDwlx();
                BigDecimal jyqlhjysr = deptCb.getJyqlhjysr();//万吨 换算成吨
                BigDecimal jcrysl = BigDecimal.valueOf(deptCb.getJcrysl());
                BigDecimal jcdwsl = BigDecimal.valueOf(deptCb.getJcdwsl());
                BigDecimal yxbzsl = BigDecimal.valueOf(deptCb.getYxbzsl());
                BigDecimal fyje = sjFybpZb.getFyje()!=null?sjFybpZb.getFyje():new BigDecimal(0);
                fyje=fyje.setScale(2,BigDecimal.ROUND_HALF_UP);
                if("油气生产单位".equals(dwlx)){
                    //计算标准
                    jyqlhjysr= deptCb.getJyqlhjysr().multiply(new BigDecimal(10000));//万吨 换算成吨
                    BigDecimal jys= jyqlhjysr.multiply(new BigDecimal(2)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    BigDecimal rys=jcrysl.multiply(new BigDecimal(1000)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    BigDecimal dws=jcdwsl.multiply(new BigDecimal(150000)).multiply(new BigDecimal(0.2)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    BigDecimal bzs=yxbzsl.multiply(new BigDecimal(20000)).multiply(new BigDecimal(0.2)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    BigDecimal sum=jys.add(rys).add(dws).add(bzs);
                    sum=sum.divide(new BigDecimal(10000)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    if(fyje.compareTo(sum)>-1){
                        msg="保存成功！费用总额"+fyje+"万元不低于"+sum+"万元。\n计算标准为："
                                +deptCb.getJyqlhjysr().setScale(2,BigDecimal.ROUND_HALF_UP)+"万吨*2元/吨*30%+"+jcrysl+"*1000元/人*30%+"+jcdwsl+"*15万元/单位*20%+"+yxbzsl+"*2万元/班站*20%="+sum;
                    }else {
                        msg="保存成功！费用总额"+fyje+"万元低于"+sum+"万元。\n计算标准为："
                                +deptCb.getJyqlhjysr().setScale(2,BigDecimal.ROUND_HALF_UP)+"万吨*2元/吨*30%+"+jcrysl+"*1000元/人*30%+"+jcdwsl+"*15万元/单位*20%+"+yxbzsl+"*2万元/班站*20%="+sum;
                    }
                }else if("专业化单位".equals(dwlx)) {
                    //计算标准
                    if(yxbzsl.equals(new BigDecimal(0))){
                        BigDecimal jys= jyqlhjysr.multiply(new BigDecimal(0.0001)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        BigDecimal rys=jcrysl.multiply(new BigDecimal(1000)).multiply(new BigDecimal(0.4)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        BigDecimal dws=jcdwsl.multiply(new BigDecimal(150000)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        BigDecimal sum=jys.add(rys).add(dws);
                        sum=sum.divide(new BigDecimal(10000)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        if(fyje.compareTo(sum)>-1){
                            msg="保存成功！费用总额"+fyje+"万元不低于"+sum+"万元。\n计算标准为："
                                    +deptCb.getJyqlhjysr().setScale(2,BigDecimal.ROUND_HALF_UP)+"万元*1‰*30%+"+jcrysl+"*1000元/人*40%+"+jcdwsl+"*15万元/单位*30%="+sum;
                        }else {
                            msg="保存成功！费用总额"+fyje+"万元低于"+sum+"万元。\n计算标准为："
                                    +deptCb.getJyqlhjysr().setScale(2,BigDecimal.ROUND_HALF_UP)+"万吨*2元/吨*30%+"+jcrysl+"*1000元/人*40%+"+jcdwsl+"*15万元/单位*30%="+sum;
                        }
                    }else {
                        BigDecimal jys= jyqlhjysr.multiply(new BigDecimal(0.0001)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        BigDecimal rys=jcrysl.multiply(new BigDecimal(1000)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        BigDecimal dws=jcdwsl.multiply(new BigDecimal(150000)).multiply(new BigDecimal(0.2)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        BigDecimal bzs=yxbzsl.multiply(new BigDecimal(20000)).multiply(new BigDecimal(0.2)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        BigDecimal sum=jys.add(rys).add(dws).add(bzs);
                        sum=sum.divide(new BigDecimal(10000)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        if(fyje.compareTo(sum)>-1){
                            msg="保存成功！费用总额"+fyje+"万元不低于"+sum+"万元。\n计算标准为："
                                    +deptCb.getJyqlhjysr().setScale(2,BigDecimal.ROUND_HALF_UP)+"万元*1‰*30%+"+jcrysl+"*1000元/人*30%+"+jcdwsl+"*15万元/单位*20%+"+yxbzsl+"*2万元/班站*20%="+sum;
                        }else {
                            msg="保存成功！费用总额"+fyje+"万元低于"+sum+"万元。\n计算标准为："
                                    +deptCb.getJyqlhjysr().setScale(2,BigDecimal.ROUND_HALF_UP)+"万吨*2元/吨*30%+"+jcrysl+"*1000元/人*30%+"+jcdwsl+"*15万元/单位*20%+"+yxbzsl+"*2万元/班站*20%="+sum;
                        }

                    }
                }else if("科研监督单位".equals(dwlx)) {
                    BigDecimal jys= jyqlhjysr.multiply(new BigDecimal(0.0001)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    BigDecimal rys=jcrysl.multiply(new BigDecimal(1000)).multiply(new BigDecimal(0.4)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    BigDecimal dws=jcdwsl.multiply(new BigDecimal(50000)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    BigDecimal sum=jys.add(rys).add(dws);
                    sum=sum.divide(new BigDecimal(10000)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    if(fyje.compareTo(sum)>-1){
                        msg="保存成功！费用总额"+fyje+"万元不低于"+sum+"万元。\n计算标准为："
                                +deptCb.getJyqlhjysr().setScale(2,BigDecimal.ROUND_HALF_UP)+"万元*1‰*30%+"+jcrysl+"*1000元/人*40%+"+jcdwsl+"*5万元/单位*30%="+sum;
                    }else {
                        msg="保存成功！费用总额"+fyje+"万元低于"+sum+"万元。\n计算标准为："
                                +deptCb.getJyqlhjysr().setScale(2,BigDecimal.ROUND_HALF_UP)+"万吨*2元/吨*30%+"+jcrysl+"*1000元/人*40%+"+jcdwsl+"*5万元/单位*30%="+sum;
                    }
                }
            }
        }
        return AjaxResult.success(msg);
    }

    /**
     * 删除三基-费用报批主
     */

    @Log(title = "三基-费用报批主", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {

        //日志
        SysUser u = SecurityUtils.getLoginUser().getUser();
        LogUtil.mkrzsc(u, "费用报批", "费用报批-年度计划", "删除年度计划上报", "删除");

        return toAjax(sjFybpZbService.deleteSjFybpZbByIds(ids));
    }



    /**
     * 提交
     */
    @Log(title = "三基-费用报批", businessType = BusinessType.UPDATE)
    @PostMapping("/tj")
    public AjaxResult tj(@RequestBody SjFybpZb sjFybpZb)
    {
        sjFybpZb.setTjr(SecurityUtils.getUsername());
        sjFybpZb.setTjrq(DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));
        return toAjax(sjFybpZbService.tj(sjFybpZb));
    }


    /**
     * 提交
     */
    @Log(title = "三基-费用报批", businessType = BusinessType.UPDATE)
    @PostMapping("/sp")
    public AjaxResult sp(@RequestBody SjFybpZb sjFybpZb)
    {
        sjFybpZb.setSpr(SecurityUtils.getUsername());
        sjFybpZb.setSprq(DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));
        return toAjax(sjFybpZbService.tj(sjFybpZb));
    }

    @GetMapping("/splist")
    public TableDataInfo splist(SjFybpZb sjFybpZb)
    {
        startPage();
        sjFybpZb.setSpcx("1");
        List<SjFybpZb> list = sjFybpZbService.selectSjFybpZbList(sjFybpZb);
        return getDataTable(list);
    }


    /**
     * 获取三基-费用报批主详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:sjFybpZb:query')")
    @GetMapping(value = "/getInfoBySp/{id}")
    public AjaxResult getInfoBySp(@PathVariable("id") Long id)
    {
        SjFybpZb sjFybpZb = sjFybpZbService.selectSjFybpZbById(id);
        SysDeptCb sysDeptCb =new SysDeptCb();
        sysDeptCb.setNd(sjFybpZb.getFyrq());
        sysDeptCb.setDeptId(sjFybpZb.getDeptId());
        SysDeptCb deptCb = sysDeptCbMapper.getInfoBydeptId(sysDeptCb);
        if(deptCb==null){
            sjFybpZb.setJl("该单位基础信息未维护，请到部门管理中进行设置！");
        }else {
            if(StringUtils.isNotEmpty( deptCb.getDwlx() )){
                String dwlx = deptCb.getDwlx();
                BigDecimal jyqlhjysr = deptCb.getJyqlhjysr();//万吨 换算成吨
                BigDecimal jcrysl = BigDecimal.valueOf(deptCb.getJcrysl());
                BigDecimal jcdwsl = BigDecimal.valueOf(deptCb.getJcdwsl());
                BigDecimal yxbzsl = BigDecimal.valueOf(deptCb.getYxbzsl());
                BigDecimal fyje = sjFybpZb.getFyje()!=null?sjFybpZb.getFyje():new BigDecimal(0);
                fyje=fyje.setScale(2,BigDecimal.ROUND_HALF_UP);
                if("油气生产单位".equals(dwlx)){
                    //计算标准
                    jyqlhjysr= deptCb.getJyqlhjysr().multiply(new BigDecimal(10000));//万吨 换算成吨
                    BigDecimal jys= jyqlhjysr.multiply(new BigDecimal(2)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    BigDecimal rys=jcrysl.multiply(new BigDecimal(1000)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    BigDecimal dws=jcdwsl.multiply(new BigDecimal(150000)).multiply(new BigDecimal(0.2)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    BigDecimal bzs=yxbzsl.multiply(new BigDecimal(20000)).multiply(new BigDecimal(0.2)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    BigDecimal sum=jys.add(rys).add(dws).add(bzs);
                    sum=sum.divide(new BigDecimal(10000)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    if(fyje.compareTo(sum)>-1){
                        sjFybpZb.setJl("费用总额"+fyje+"万元不低于"+sum+"万元。");
                        sjFybpZb.setJlflag(true);
                    }else {
                        sjFybpZb.setJl("费用总额"+fyje+"万元低于"+sum+"万元。");
                        sjFybpZb.setJlflag(false);
                    }
                    sjFybpZb.setJsbz("计算标准为："
                            +deptCb.getJyqlhjysr().setScale(2,BigDecimal.ROUND_HALF_UP)+"万吨*2元/吨*30%+"+jcrysl+"*1000元/人*30%+"+jcdwsl+"*15万元/单位*20%+"+yxbzsl+"*2万元/班站*20%="+sum);
                    sjFybpZb.setTqbz("交油气量（万吨）×2 元/吨×30%+基层人员数量×1000 元/人×30%+\n" +
                            "基层单位数量×15 万元/单位×20%+一线班站数量×2 万元/班站×20%");

                }else if("专业化单位".equals(dwlx)) {
                    //计算标准
                    if(yxbzsl.equals(new BigDecimal(0))){
                        BigDecimal jys= jyqlhjysr.multiply(new BigDecimal(0.0001)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        BigDecimal rys=jcrysl.multiply(new BigDecimal(1000)).multiply(new BigDecimal(0.4)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        BigDecimal dws=jcdwsl.multiply(new BigDecimal(150000)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        BigDecimal sum=jys.add(rys).add(dws);
                        sum=sum.divide(new BigDecimal(10000)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        if(fyje.compareTo(sum)>-1){
                            sjFybpZb.setJl("费用总额"+fyje+"万元不低于"+sum+"万元。");
                            sjFybpZb.setJlflag(true);
                        }else {
                            sjFybpZb.setJl("费用总额"+fyje+"万元低于"+sum+"万元。");
                            sjFybpZb.setJlflag(false);
                        }
                        sjFybpZb.setJsbz("计算标准为："
                                +deptCb.getJyqlhjysr().setScale(2,BigDecimal.ROUND_HALF_UP)+"万吨*2元/吨*30%+"+jcrysl+"*1000元/人*40%+"+jcdwsl+"*15万元/单位*30%="+sum);
                        sjFybpZb.setTqbz("经营收入（万元）×1‰×30%+基层人员数量×1000 元/人×40%+基层\n" +
                                "单位数量×15 万元/单位×30%");
                    }else {
                        BigDecimal jys= jyqlhjysr.multiply(new BigDecimal(0.0001)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        BigDecimal rys=jcrysl.multiply(new BigDecimal(1000)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        BigDecimal dws=jcdwsl.multiply(new BigDecimal(150000)).multiply(new BigDecimal(0.2)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        BigDecimal bzs=yxbzsl.multiply(new BigDecimal(20000)).multiply(new BigDecimal(0.2)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        BigDecimal sum=jys.add(rys).add(dws).add(bzs);
                        sum=sum.divide(new BigDecimal(10000)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        if(fyje.compareTo(sum)>-1){
                            sjFybpZb.setJl("费用总额"+fyje+"万元不低于"+sum+"万元。");
                            sjFybpZb.setJlflag(true);
                        }else {
                            sjFybpZb.setJl("费用总额"+fyje+"万元低于"+sum+"万元。");
                            sjFybpZb.setJlflag(false);
                        }
                        sjFybpZb.setJsbz("计算标准为："
                                +deptCb.getJyqlhjysr().setScale(2,BigDecimal.ROUND_HALF_UP)+"万吨*2元/吨*30%+"+jcrysl+"*1000元/人*30%+"+jcdwsl+"*15万元/单位*20%+"+yxbzsl+"*2万元/班站*20%="+sum);
                        sjFybpZb.setTqbz("经营收入（万元）×1‰×30%+基层人员数量×1000 元/人×30%+基层\n" +
                                "单位数量×15 万元/单位×20%+一线班站数量×2 万元/班站×20%");
                    }
                }else if("科研监督单位".equals(dwlx)) {
                    BigDecimal jys= jyqlhjysr.multiply(new BigDecimal(0.0001)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    BigDecimal rys=jcrysl.multiply(new BigDecimal(1000)).multiply(new BigDecimal(0.4)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    BigDecimal dws=jcdwsl.multiply(new BigDecimal(50000)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    BigDecimal sum=jys.add(rys).add(dws);
                    sum=sum.divide(new BigDecimal(10000)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    if(fyje.compareTo(sum)>-1){
                        sjFybpZb.setJl("费用总额"+fyje+"万元不低于"+sum+"万元。");
                        sjFybpZb.setJlflag(true);
                    }else {
                        sjFybpZb.setJl("费用总额"+fyje+"万元低于"+sum+"万元。");
                        sjFybpZb.setJlflag(false);
                    }
                    sjFybpZb.setJsbz("计算标准为："
                            +deptCb.getJyqlhjysr().setScale(2,BigDecimal.ROUND_HALF_UP)+"万吨*2元/吨*30%+"+jcrysl+"*1000元/人*40%+"+jcdwsl+"*5万元/单位*30%="+sum);
                    sjFybpZb.setTqbz("经营收入（万元）×1‰×30%+基层人员数量×1000 元/人×40%+基层\n" +
                            "单位数量×5 万元/单位×30%");
                }
            }
        }

        return success(sjFybpZb);
    }


    /**
     * 获取三基-费用报批 参考标准
     */
    @GetMapping(value = "/getCkbz")
    public AjaxResult getCkbz(SjFybpZb sjFybpZb)
    {
        SysDeptCb sysDeptCb =new SysDeptCb();
        sysDeptCb.setNd(sjFybpZb.getFyrq());
        sysDeptCb.setDeptId(sjFybpZb.getDeptId());
        SysDeptCb deptCb = sysDeptCbMapper.getInfoBydeptId(sysDeptCb);
        if(deptCb==null){
            sjFybpZb.setJl("该单位基础信息未维护，请到部门管理中进行设置！");
        }else {
            if(StringUtils.isNotEmpty( deptCb.getDwlx() )){
                String dwlx = deptCb.getDwlx();
                BigDecimal jyqlhjysr = deptCb.getJyqlhjysr();//万吨 换算成吨
                BigDecimal jcrysl = BigDecimal.valueOf(deptCb.getJcrysl());
                BigDecimal jcdwsl = BigDecimal.valueOf(deptCb.getJcdwsl());
                BigDecimal yxbzsl = BigDecimal.valueOf(deptCb.getYxbzsl());
                BigDecimal fyje = sjFybpZb.getFyje()!=null?sjFybpZb.getFyje():new BigDecimal(0);
                fyje=fyje.setScale(2,BigDecimal.ROUND_HALF_UP);
                if("油气生产单位".equals(dwlx)){
                    //计算标准
                    jyqlhjysr= deptCb.getJyqlhjysr().multiply(new BigDecimal(10000));//万吨 换算成吨
                    BigDecimal jys= jyqlhjysr.multiply(new BigDecimal(2)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    BigDecimal rys=jcrysl.multiply(new BigDecimal(1000)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    BigDecimal dws=jcdwsl.multiply(new BigDecimal(150000)).multiply(new BigDecimal(0.2)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    BigDecimal bzs=yxbzsl.multiply(new BigDecimal(20000)).multiply(new BigDecimal(0.2)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    BigDecimal sum=jys.add(rys).add(dws).add(bzs);
                    sum=sum.divide(new BigDecimal(10000)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    if(fyje.compareTo(sum)>-1){
                        sjFybpZb.setJl("费用总额"+fyje+"万元不低于"+sum+"万元。");
                        sjFybpZb.setJlflag(true);
                    }else {
                        sjFybpZb.setJl("费用总额"+fyje+"万元低于"+sum+"万元。");
                        sjFybpZb.setJlflag(false);
                    }
                    sjFybpZb.setJsbz("计算标准为："
                            +deptCb.getJyqlhjysr().setScale(2,BigDecimal.ROUND_HALF_UP)+"万吨*2元/吨*30%+"+jcrysl+"*1000元/人*30%+"+jcdwsl+"*15万元/单位*20%+"+yxbzsl+"*2万元/班站*20%="+sum);
                    sjFybpZb.setTqbz("交油气量（万吨）×2 元/吨×30%+基层人员数量×1000 元/人×30%+\n" +
                            "基层单位数量×15 万元/单位×20%+一线班站数量×2 万元/班站×20%");

                }else if("专业化单位".equals(dwlx)) {
                    //计算标准
                    if(yxbzsl.equals(new BigDecimal(0))){
                        BigDecimal jys= jyqlhjysr.multiply(new BigDecimal(0.0001)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        BigDecimal rys=jcrysl.multiply(new BigDecimal(1000)).multiply(new BigDecimal(0.4)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        BigDecimal dws=jcdwsl.multiply(new BigDecimal(150000)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        BigDecimal sum=jys.add(rys).add(dws);
                        sum=sum.divide(new BigDecimal(10000)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        if(fyje.compareTo(sum)>-1){
                            sjFybpZb.setJl("费用总额"+fyje+"万元不低于"+sum+"万元。");
                            sjFybpZb.setJlflag(true);
                        }else {
                            sjFybpZb.setJl("费用总额"+fyje+"万元低于"+sum+"万元。");
                            sjFybpZb.setJlflag(false);
                        }
                        sjFybpZb.setJsbz("计算标准为："
                                +deptCb.getJyqlhjysr().setScale(2,BigDecimal.ROUND_HALF_UP)+"万吨*2元/吨*30%+"+jcrysl+"*1000元/人*40%+"+jcdwsl+"*15万元/单位*30%="+sum);
                        sjFybpZb.setTqbz("经营收入（万元）×1‰×30%+基层人员数量×1000 元/人×40%+基层\n" +
                                "单位数量×15 万元/单位×30%");
                    }else {
                        BigDecimal jys= jyqlhjysr.multiply(new BigDecimal(0.0001)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        BigDecimal rys=jcrysl.multiply(new BigDecimal(1000)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        BigDecimal dws=jcdwsl.multiply(new BigDecimal(150000)).multiply(new BigDecimal(0.2)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        BigDecimal bzs=yxbzsl.multiply(new BigDecimal(20000)).multiply(new BigDecimal(0.2)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        BigDecimal sum=jys.add(rys).add(dws).add(bzs);
                        sum=sum.divide(new BigDecimal(10000)).setScale(2,BigDecimal.ROUND_HALF_UP);
                        if(fyje.compareTo(sum)>-1){
                            sjFybpZb.setJl("费用总额"+fyje+"万元不低于"+sum+"万元。");
                            sjFybpZb.setJlflag(true);
                        }else {
                            sjFybpZb.setJl("费用总额"+fyje+"万元低于"+sum+"万元。");
                            sjFybpZb.setJlflag(false);
                        }
                        sjFybpZb.setJsbz("计算标准为："
                                +deptCb.getJyqlhjysr().setScale(2,BigDecimal.ROUND_HALF_UP)+"万吨*2元/吨*30%+"+jcrysl+"*1000元/人*30%+"+jcdwsl+"*15万元/单位*20%+"+yxbzsl+"*2万元/班站*20%="+sum);
                        sjFybpZb.setTqbz("经营收入（万元）×1‰×30%+基层人员数量×1000 元/人×30%+基层\n" +
                                "单位数量×15 万元/单位×20%+一线班站数量×2 万元/班站×20%");
                    }
                }else if("科研监督单位".equals(dwlx)) {
                    BigDecimal jys= jyqlhjysr.multiply(new BigDecimal(0.0001)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    BigDecimal rys=jcrysl.multiply(new BigDecimal(1000)).multiply(new BigDecimal(0.4)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    BigDecimal dws=jcdwsl.multiply(new BigDecimal(50000)).multiply(new BigDecimal(0.3)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    BigDecimal sum=jys.add(rys).add(dws);
                    sum=sum.divide(new BigDecimal(10000)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    if(fyje.compareTo(sum)>-1){
                        sjFybpZb.setJl("费用总额"+fyje+"万元不低于"+sum+"万元。");
                        sjFybpZb.setJlflag(true);
                    }else {
                        sjFybpZb.setJl("费用总额"+fyje+"万元低于"+sum+"万元。");
                        sjFybpZb.setJlflag(false);
                    }
                    sjFybpZb.setJsbz("计算标准为："
                            +deptCb.getJyqlhjysr().setScale(2,BigDecimal.ROUND_HALF_UP)+"万吨*2元/吨*30%+"+jcrysl+"*1000元/人*40%+"+jcdwsl+"*5万元/单位*30%="+sum);
                    sjFybpZb.setTqbz("经营收入（万元）×1‰×30%+基层人员数量×1000 元/人×40%+基层\n" +
                            "单位数量×5 万元/单位×30%");
                }
            }
        }

        return success(sjFybpZb);
    }



    //导入模板 下载
    @PostMapping("/exportDrmb")
    public void exportDrmb(HttpServletResponse response, SjFybpZb sjFybpZb)
    {
        Workbook exl = null;
        OutputStream out = null;
        InputStream in  = null;
        try {

            in =this.getClass().getResourceAsStream("/static/fybp_drmb.xlsx");
            exl = WorkbookFactory.create(in);
            out = response.getOutputStream();
            response.reset();
            String filename = URLEncoder.encode("费用上报导入模板.xlsx", "UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + filename);
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);

            Sheet sheet = exl.getSheet("Sheet1");
            CellStyle cellStyle = exl.createCellStyle();
            cellStyle.setWrapText(true);
            cellStyle.setAlignment(HorizontalAlignment.LEFT);
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            cellStyle.setBorderBottom(BorderStyle.THIN);
            cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
            cellStyle.setBorderLeft(BorderStyle.THIN);
            cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
            cellStyle.setBorderRight(BorderStyle.THIN);
            cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
            cellStyle.setBorderTop(BorderStyle.THIN);
            cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());

            exl.write(out);
            exl.close();
        }catch (Exception e){
            e.getMessage();
            throw new BusinessException("导出Excel失败，请联系网站管理员！");
        }finally {
            if (exl != null) {
                try {
                    exl.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }

    }
    //导入
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception{
        List<SjFybp> list = new ArrayList<>();
        XSSFWorkbook workbook = null;
        try {
            //装载流
            workbook = new XSSFWorkbook(file.getInputStream());

            // 获取一个工作表，下标从0开始
            XSSFSheet sheet = workbook.getSheetAt(0);
            int lastRowNum = sheet.getLastRowNum();

            // 通过循环，逐行取出表中每行数据
            for(int i=0;i<=4;i++) {//跳过第一行和第二行
                if(i <= 3){
                    continue;
                }
                // 获取行
                XSSFRow row = sheet.getRow(i);

                SjFybp cb1 = new SjFybp();
                cb1.setFylx("三基标杆打造");
                //项目金额
                String fyje1 = new DataFormatter().formatCellValue(row.getCell(3));
                if (null == fyje1  || fyje1.trim().isEmpty()) {
                    fyje1="0";
                }
                cb1.setFyje(new BigDecimal(fyje1));
                //项目内容
                String fyms1 = new DataFormatter().formatCellValue(row.getCell(4));
                cb1.setFyms(fyms1);
                list.add(cb1);

                SjFybp cb2 = new SjFybp();
                cb2.setFylx("三个最强全员练兵");
                //项目金额
                String fyje2 = new DataFormatter().formatCellValue(row.getCell(5));
                if (null == fyje2  || fyje2.trim().isEmpty()) {
                    fyje2="0";
                }
                cb2.setFyje(new BigDecimal(fyje2));
                //项目内容
                String fyms2 = new DataFormatter().formatCellValue(row.getCell(6));
                cb2.setFyms(fyms2);
                list.add(cb2);

                SjFybp cb3 = new SjFybp();
                cb3.setFylx("三标建设5S管理");
                //项目金额
                String fyje3 = new DataFormatter().formatCellValue(row.getCell(7));
                if (null == fyje3  || fyje3.trim().isEmpty()) {
                    fyje3="0";
                }
                cb3.setFyje(new BigDecimal(fyje3));
                //项目内容
                String fyms3 = new DataFormatter().formatCellValue(row.getCell(8));
                cb3.setFyms(fyms3);
                list.add(cb3);

                SjFybp cb4 = new SjFybp();
                cb4.setFylx("基层班站建设");
                //项目金额
                String fyje4 = new DataFormatter().formatCellValue(row.getCell(9));
                if (null == fyje4  || fyje4.trim().isEmpty()) {
                    fyje4="0";
                }
                cb4.setFyje(new BigDecimal(fyje4));
                //项目内容
                String fyms4 = new DataFormatter().formatCellValue(row.getCell(10));
                cb4.setFyms(fyms4);
                list.add(cb4);

                SjFybp cb5 = new SjFybp();
                cb5.setFylx("其他");
                //项目金额
                String fyje5 = new DataFormatter().formatCellValue(row.getCell(11));
                if (null == fyje5  || fyje5.trim().isEmpty()) {
                    fyje5="0";
                }
                cb5.setFyje(new BigDecimal(fyje5));
                //项目内容
                String fyms5 = new DataFormatter().formatCellValue(row.getCell(12));
                cb5.setFyms(fyms5);
                list.add(cb5);
            }
//            if(list.size()>0){
//                sjGfjsZsdwkhpjService.pledit(list);
//            }
        } catch (Exception e) {
            e.getMessage();
            e.printStackTrace();
            throw new BusinessException("导入Excel失败，请检查数据格式，尤其是金额！");

        } finally {
            workbook.close();
        }
        return AjaxResult.success(list);
    }

}
