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.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 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)
    {
        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)
    {
        return success(sjFybpZbService.selectSjFybpZbById(id));
    }

    /**
     * 新增三基-费用报批主
     */
    @Log(title = "三基-费用报批主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SjFybpZb sjFybpZb)
    {
        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)
    {
        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)
    {
        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<=lastRowNum;i++) {//跳过第一行和第二行
                if(i==0 || i==1){
                    continue;
                }
                // 获取行
                XSSFRow row = sheet.getRow(i);
                SjFybp cb = new SjFybp();
                System.out.println(row);

                //项目金额
                String fylx = new DataFormatter().formatCellValue(row.getCell(0));
                cb.setFylx(fylx);

                //项目金额
                String fyje = new DataFormatter().formatCellValue(row.getCell(1));
                cb.setFyje(new BigDecimal(fyje));

                //项目内容
                String fyms = new DataFormatter().formatCellValue(row.getCell(2));
                cb.setFyms(fyms);

                list.add(cb);
            }
//            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);
    }

}
