package cn.timer.api.controller.sbgjj;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;

import cn.hutool.core.util.StrUtil;
import cn.timer.api.bean.sbgjj.SbgjjAdminCbry;
import cn.timer.api.bean.sbgjj.SbgjjAreaInfo;
import cn.timer.api.bean.sbgjj.SbgjjAssoCbfa;
import cn.timer.api.bean.sbgjj.SbgjjAssoCbfzmx;
import cn.timer.api.bean.sbgjj.SbgjjAssoDetailed;
import cn.timer.api.bean.sbgjj.SbgjjAssoYjzd;
import cn.timer.api.bean.sbgjj.SbgjjTypeDetails;
import cn.timer.api.bean.sbgjj.SbgjjYjCsbh;
import cn.timer.api.bean.yggl.YgglMainEmp;
import cn.timer.api.config.annotation.CurrentUser;
import cn.timer.api.config.annotation.UserBean;
import cn.timer.api.dao.sbgjj.SbgjjAdminCbryMapper;
import cn.timer.api.dao.sbgjj.SbgjjAreaInfoMapper;
import cn.timer.api.dao.sbgjj.SbgjjAssoCbfaMapper;
import cn.timer.api.dao.sbgjj.SbgjjAssoCbfzmxMapper;
import cn.timer.api.dao.sbgjj.SbgjjAssoDetailedMapper;
import cn.timer.api.dao.sbgjj.SbgjjAssoYjzdMapper;
import cn.timer.api.dao.sbgjj.SbgjjTypeDetailsMapper;
import cn.timer.api.dao.sbgjj.SbgjjYjCsbhMapper;
import cn.timer.api.dto.sbgjj.InspersonnelDto;
import cn.timer.api.dto.sbgjj.InsuranceSchemeDto;
import cn.timer.api.dto.sbgjj.InsureDto;
import cn.timer.api.dto.sbgjj.InsuredPersonnelDto;
import cn.timer.api.dto.sbgjj.MonthlyCheckoutSheetDto;
import cn.timer.api.dto.sbgjj.MonthlyStatementDto;
import cn.timer.api.dto.sbgjj.SearchPlanDto;
import cn.timer.api.dto.sbgjj.SocialSecurityFundDto;
import cn.timer.api.dto.sbgjj.SocialfundDto;
import cn.timer.api.dto.sbgjj.StopimmediatelyDto;
import cn.timer.api.utils.ResponseResult;
import cn.timer.api.utils.Result;
import cn.timer.api.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "社保公积金Api")
@RestController
@RequestMapping(value = "/soc", produces = { "application/json" })
public class SocialSecurityFundController {
	@Autowired
	private SbgjjAreaInfoMapper sbgjjareainfomapper;
	@Autowired
	private SbgjjTypeDetailsMapper sbgjjtypedetailsmapper;
	@Autowired
	private SbgjjYjCsbhMapper sbgjjyjcsbhmapper;
	//参保方案
	@Autowired
	private SbgjjAssoCbfaMapper sbgjjassocbfamapper;
	//参保方案明细
	@Autowired
	private SbgjjAssoCbfzmxMapper sbgjjassocbfzmxmapper;
	
	/*********/
	//参保人员
	@Autowired
	private SbgjjAdminCbryMapper sbgjjadmincbrymapper;
	//月结账单
	@Autowired
	private SbgjjAssoYjzdMapper sbgjjassoyjzdmapper;
	
	
	
	@GetMapping(value = "/basicdatalist/{insuredcities}")
	@ApiOperation(value = "社保公积金", httpMethod = "GET", notes = "接口发布说明")
	public ResponseResult BasicdataList(@PathVariable String insuredcities) {
		List<SbgjjAreaInfo> xcgladminxzz = new LambdaQueryChainWrapper<SbgjjAreaInfo>(sbgjjareainfomapper).like(SbgjjAreaInfo::getName, insuredcities).list();
		return new ResponseResult().success("获取薪资组信息成功", xcgladminxzz);
	}


	@GetMapping(value = "/city")
	@ApiOperation(value = "社保公积金城市", httpMethod = "GET", notes = "接口发布说明")
	public ResponseResult Citylist() {
		
		List<SbgjjYjCsbh> xcgladminxzz = new LambdaQueryChainWrapper<SbgjjYjCsbh>(sbgjjyjcsbhmapper).list();
		return new ResponseResult().success("获取社保公积金城市信息成功", xcgladminxzz);
	}


	@GetMapping(value = "/sbgjj/{name}")
	@ApiOperation(value = "社保公积金", httpMethod = "GET", notes = "接口发布说明")
	public ResponseResult SocialSecurityFund(@PathVariable String name) {
		
		SocialSecurityFundDto ssf = new SocialSecurityFundDto();
		
		List<SbgjjTypeDetails> sblist = null;
		
		List<SbgjjTypeDetails> sblist1 = sbgjjtypedetailsmapper.SocialSecurityFund(name,1);//社保
		
		if(sblist1.size() > 0) {//没有数据
			sblist = sblist1;//社保
		}else {
			sblist = sbgjjtypedetailsmapper.SocialBlankReference(1);//社保
		}
		
		List<SocialfundDto> socials = new ArrayList<SocialfundDto>();
		for(SbgjjTypeDetails sbs:sblist) {
			SocialfundDto socia = new SocialfundDto();
			socia.setName(sbs.getName());;// 社保或公积金名字
			socia.setBaseDown(String.valueOf(sbs.getBaseDown()));;// 基数范围下限
			socia.setBaseUp(String.valueOf(sbs.getBaseUp()));;// 基数范围上限
			socia.setCompanyRatio(String.valueOf(sbs.getCompanyRatio()));;// 公司比例
			socia.setCompanyFixedMoney(sbs.getCompanyFixedMoney());;// 公司固定金额
			socia.setPersonRatio(String.valueOf(sbs.getPersonRatio()));;// 个人比例
			socia.setPersonFixedMoney(sbs.getPersonFixedMoney());;// 个人固定金额
			socials.add(socia);
		}
		ssf.setSocial(socials);
		
		List<SbgjjTypeDetails> gjjlist = null;
		
		List<SbgjjTypeDetails> gjjlist1 = sbgjjtypedetailsmapper.SocialSecurityFund(name,2);//公积金
		if(gjjlist1.size() > 0) {
			gjjlist = gjjlist1;
		}else {
			gjjlist = sbgjjtypedetailsmapper.SocialBlankReference(2);//公积金
		}
		
		List<SocialfundDto> funds = new ArrayList<SocialfundDto>();
		for(SbgjjTypeDetails gjjs:gjjlist) {
			SocialfundDto fund = new SocialfundDto();
			fund.setName(gjjs.getName());;// 社保或公积金名字
			fund.setBaseDown(String.valueOf(gjjs.getBaseDown()));;// 基数范围下限
			fund.setBaseUp(String.valueOf(gjjs.getBaseUp()));;// 基数范围上限
			fund.setCompanyRatio(String.valueOf(gjjs.getCompanyRatio()));;// 公司比例
			fund.setCompanyFixedMoney(gjjs.getCompanyFixedMoney());;// 公司固定金额
			fund.setPersonRatio(String.valueOf(gjjs.getPersonRatio()));;// 个人比例
			fund.setPersonFixedMoney(gjjs.getPersonFixedMoney());;// 个人固定金额
			funds.add(fund);
		}
		ssf.setFund(funds);
		
		
		return new ResponseResult().success("获取社保公积金成功", ssf);
	}

	/**
	 *	切换社保
	 */
	@GetMapping(value = "/switchsocial/{id}")
	@ApiOperation(value = "切换社保", httpMethod = "GET", notes = "接口发布说明")
	public ResponseResult switchsocial(@PathVariable int id) {
		List<SocialfundDto> qhsb =  sbgjjtypedetailsmapper.Socialfundlist(id);
		return new ResponseResult().success("切换社保成功", qhsb);
	}
	/**
	 *	切换公积金
	 */
	@GetMapping(value = "/switchfund/{id}")
	@ApiOperation(value = "切换公积金", httpMethod = "GET", notes = "接口发布说明")
	public ResponseResult switchfund(@PathVariable int id) {
		List<SocialfundDto> qhgjj =  sbgjjtypedetailsmapper.Socialfundlist(id);
		return new ResponseResult().success("切换公积金成功", qhgjj);
	}


	@PostMapping(value = "/insueme")
	@ApiOperation(value = "添加参保方案", httpMethod = "POST", notes = "接口发布说明")
	public ResponseResult InsuranceScheme(@CurrentUser UserBean userBean,@RequestBody InsuranceSchemeDto insura) {
		
		SbgjjAssoCbfa cbfa = SbgjjAssoCbfa.builder().build();
		cbfa.setCbmcName(insura.getName());//参保名称
		cbfa.setCbcs(insura.getInsuredcities());//参保城市
		cbfa.setSbcbfzmc(insura.getShebaoname());//社保参保方案名称
		cbfa.setGjjcbfamc(insura.getGongjijinname());//公积金参保方案名称
		cbfa.setFwf(Double.valueOf(insura.getFwf()));//服务费 (元/人/月)
		cbfa.setSbSbgjjid(insura.getShebaoid());//社保(公积金)方案库id
		cbfa.setGjjSbgjjid(insura.getGongjijinid());//(社保)公积金方案库id
		
		cbfa.setSbmin(Double.valueOf(insura.getSbmin()));
		cbfa.setGjjmin(Double.valueOf(insura.getGjjmin()));
		
		cbfa.setSbmax(Double.valueOf(insura.getSbmax()));
		cbfa.setGjjmax(Double.valueOf(insura.getGjjmax()));
		
		cbfa.setQyid(userBean.getOrgCode());
		sbgjjassocbfamapper.sbgjjassocbfainsert(cbfa);
		int cbfaid = cbfa.getId();
		
		
		List<SbgjjAssoCbfzmx> fzmxlist=new ArrayList<SbgjjAssoCbfzmx>();
		SocialfundDto[] shebaos = insura.getShebao();//
		if(shebaos.length>0){
			for(int p=0;p<shebaos.length;p++){
				SbgjjAssoCbfzmx cbf = new SbgjjAssoCbfzmx();
				cbf.setXz(shebaos[p].getName());//险种
				cbf.setJsxx(shebaos[p].getBaseDown());//基数下限
				cbf.setJssx(shebaos[p].getBaseUp());//基数上限
				cbf.setGjbl(shebaos[p].getCompanyRatio());//公司比例
				cbf.setGsgdje(shebaos[p].getCompanyFixedMoney());//公司固定金额
				cbf.setGsqzgz(1);//公司取整规则  1:四舍五入
				cbf.setGrbl(shebaos[p].getPersonRatio());//个人比例
				cbf.setGrgdje(shebaos[p].getPersonFixedMoney());//个人固定金额
				cbf.setGrqzgz(1);//个人取整规则 1:四舍五入
				cbf.setType(1);//类型 1:社保 2:公积金
				cbf.setTjsjTime(new Date().getTime());//添加时间
				cbf.setTjryNum(userBean.getEmpNum());//添加人员
				cbf.setGssbhj(1);//公司社保合计 规则(四舍五入) 1:四舍五入
				cbf.setGrsbhj(1);//个人社保合计 规则(四舍五入) 1:四舍五入
				cbf.setGsgjjhj(1);//公司公积金合计 规则(四舍五入) 1:四舍五入
				cbf.setGrgjjhj(1);//个人公积金合计 规则(四舍五入) 1:四舍五入
				cbf.setCbfaid(cbfaid);//参保方案id 
				fzmxlist.add(cbf);
			}
		}
		if(fzmxlist.size()>0){
			sbgjjassocbfzmxmapper.insertsbgjjassocbfzmxList(fzmxlist);
		}
		
		
		List<SbgjjAssoCbfzmx> gjjlist=new ArrayList<SbgjjAssoCbfzmx>();
		SocialfundDto[] gongjijins = insura.getGongjijin();//
		if(gongjijins.length>0){
			for(int p=0;p<gongjijins.length;p++){
				SbgjjAssoCbfzmx cbf = new SbgjjAssoCbfzmx();
				cbf.setXz(gongjijins[p].getName());//险种
				cbf.setJsxx(gongjijins[p].getBaseDown());//基数下限
				cbf.setJssx(gongjijins[p].getBaseUp());//基数上限
				cbf.setGjbl(gongjijins[p].getCompanyRatio());//公司比例
				cbf.setGsgdje(gongjijins[p].getCompanyFixedMoney());//公司固定金额
				cbf.setGsqzgz(1);//公司取整规则  1:四舍五入
				cbf.setGrbl(gongjijins[p].getPersonRatio());//个人比例
				cbf.setGrgdje(gongjijins[p].getPersonFixedMoney());//个人固定金额
				cbf.setGrqzgz(1);//个人取整规则 1:四舍五入
				cbf.setType(2);//类型 1:社保 2:公积金
				cbf.setTjsjTime(new Date().getTime());//添加时间
				cbf.setTjryNum(userBean.getEmpNum());//添加人员
				cbf.setGssbhj(1);//公司社保合计 规则(四舍五入) 1:四舍五入
				cbf.setGrsbhj(1);//个人社保合计 规则(四舍五入) 1:四舍五入
				cbf.setGsgjjhj(1);//公司公积金合计 规则(四舍五入) 1:四舍五入
				cbf.setGrgjjhj(1);//个人公积金合计 规则(四舍五入) 1:四舍五入
				cbf.setCbfaid(cbfaid);//参保方案id 
				gjjlist.add(cbf);
			}
		}
		if(gjjlist.size()>0){
			sbgjjassocbfzmxmapper.insertsbgjjassocbfzmxList(gjjlist);
		}
		
		return new ResponseResult().success("成功", cbfa);
	}


	@PostMapping(value = "/programme")
	@ApiOperation(value = "员工搜索/分页", httpMethod = "POST", notes = "接口发布说明")
	public Result<Object> programme(@CurrentUser UserBean userBean,@RequestBody SearchPlanDto searchp) {
		
		String c = searchp.getCityName();

		Page<SbgjjAssoCbfa> page = new Page<SbgjjAssoCbfa>(
				searchp.getCurrentPage() == null ? 1 : searchp.getCurrentPage(),
						searchp.getTotalPage() == null ? 10 : searchp.getTotalPage());

		QueryWrapper<SbgjjAssoCbfa> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("qyid",userBean.getOrgCode()).and(!StrUtil.hasBlank(c), wq -> wq.likeRight("cbcs", c));
		
		IPage<SbgjjAssoCbfa> sbgjjassocbfaPage = SbgjjAssoCbfa.builder().build().selectPage(page, queryWrapper);
		List<SbgjjAssoCbfa> sbgjjassocbfaEmps = sbgjjassocbfaPage.getRecords();
		sbgjjassocbfaPage.getCurrent();
		sbgjjassocbfaPage.getPages();
		sbgjjassocbfaPage.getTotal();
		sbgjjassocbfaPage.getSize();
		
		return ResultUtil.data(sbgjjassocbfaPage, sbgjjassocbfaEmps, "方案搜索成功");
	}


	@DeleteMapping(value = "/delschemegroup/{id}")
	@ApiOperation(value = "删除方案组", httpMethod = "DELETE", notes = "接口发布说明")
	public ResponseResult delSchemeGroup(@PathVariable Integer id) {
		
		SbgjjAssoCbfa.builder().build().deleteById(id);
		sbgjjassocbfzmxmapper.deleteBycbfaid(id);
		
		return new ResponseResult().success("删除成功", id);
		
	}


	@GetMapping(value = "/initialization/{id}")
	@ApiOperation(value = "社保公积金初始化", httpMethod = "GET", notes = "接口发布说明")
	public ResponseResult Initialization(@PathVariable Integer id) {
		
		SocialSecurityFundDto ssf = new SocialSecurityFundDto();
		
//		List<SbgjjAssoCbfzmx> sblist = sbgjjtypedetailsmapper.SocialSecurityFund(name,1);//社保
		
		List<SbgjjAssoCbfzmx> sblist = new LambdaQueryChainWrapper<SbgjjAssoCbfzmx>(sbgjjassocbfzmxmapper).eq(SbgjjAssoCbfzmx::getCbfaid, id).eq(SbgjjAssoCbfzmx::getType, 1).list();
		
		List<SocialfundDto> socials = new ArrayList<SocialfundDto>();
		for(SbgjjAssoCbfzmx sbs:sblist) {
			SocialfundDto socia = new SocialfundDto();
			socia.setName(sbs.getXz());;// 社保或公积金名字
			socia.setBaseDown(sbs.getJsxx());;// 基数范围下限
			socia.setBaseUp(sbs.getJssx());;// 基数范围上限
			socia.setCompanyRatio(sbs.getGjbl());;// 公司比例
			socia.setCompanyFixedMoney(sbs.getGsgdje());;// 公司固定金额
			socia.setPersonRatio(sbs.getGrbl());;// 个人比例
			socia.setPersonFixedMoney(sbs.getGrgdje());;// 个人固定金额
			socials.add(socia);
		}
		ssf.setSocial(socials);
		
//		List<SbgjjTypeDetails> gjjlist = sbgjjtypedetailsmapper.SocialSecurityFund(name,2);//公积金
		List<SbgjjAssoCbfzmx> gjjlist = new LambdaQueryChainWrapper<SbgjjAssoCbfzmx>(sbgjjassocbfzmxmapper).eq(SbgjjAssoCbfzmx::getCbfaid, id).eq(SbgjjAssoCbfzmx::getType, 2).list();
		List<SocialfundDto> funds = new ArrayList<SocialfundDto>();
		for(SbgjjAssoCbfzmx gjjs:gjjlist) {
			SocialfundDto fund = new SocialfundDto();
			fund.setName(gjjs.getXz());;// 社保或公积金名字
			fund.setBaseDown(gjjs.getJsxx());;// 基数范围下限
			fund.setBaseUp(gjjs.getJssx());;// 基数范围上限
			fund.setCompanyRatio(gjjs.getGjbl());;// 公司比例
			fund.setCompanyFixedMoney(gjjs.getGsgdje());;// 公司固定金额
			fund.setPersonRatio(gjjs.getGrbl());;// 个人比例
			fund.setPersonFixedMoney(gjjs.getGrgdje());;// 个人固定金额
			funds.add(fund);
		}
		ssf.setFund(funds);
		
		return new ResponseResult().success("获取社保公积金成功", ssf);
	}


	@PutMapping(value = "/updaschemegroup")
	@ApiOperation(value = "修改方案组", httpMethod = "PUT", notes = "接口发布说明")
	public ResponseResult updaSchemeGroup(@CurrentUser UserBean userBean,@RequestBody InsuranceSchemeDto insura) {
		
		
		SbgjjAssoCbfa cbfa = SbgjjAssoCbfa.builder().build();
		cbfa.setCbmcName(insura.getName());//参保名称
		cbfa.setCbcs(insura.getInsuredcities());//参保城市
		cbfa.setSbcbfzmc(insura.getShebaoname());//社保参保方案名称
		cbfa.setGjjcbfamc(insura.getGongjijinname());//公积金参保方案名称
		cbfa.setFwf(Double.valueOf(insura.getFwf()));//服务费 (元/人/月)
		cbfa.setSbSbgjjid(insura.getShebaoid());//社保(公积金)方案库id
		cbfa.setGjjSbgjjid(insura.getGongjijinid());//(社保)公积金方案库id
		cbfa.setQyid(userBean.getOrgCode());
		cbfa.setId(insura.getId());
		cbfa.setSbmin(Double.valueOf(insura.getSbmin()));
		cbfa.setGjjmin(Double.valueOf(insura.getGjjmin()));
		
		cbfa.setSbmax(Double.valueOf(insura.getSbmax()));
		cbfa.setGjjmax(Double.valueOf(insura.getGjjmax()));
		
		cbfa.updateById();
		
		sbgjjassocbfzmxmapper.deleteBycbfaid(insura.getId());
		
		List<SbgjjAssoCbfzmx> fzmxlist=new ArrayList<SbgjjAssoCbfzmx>();
		SocialfundDto[] shebaos = insura.getShebao();//
		if(shebaos.length>0){
			for(int p=0;p<shebaos.length;p++){
				SbgjjAssoCbfzmx cbf = new SbgjjAssoCbfzmx();
				cbf.setXz(shebaos[p].getName());//险种
				cbf.setJsxx(shebaos[p].getBaseDown());//基数下限
				cbf.setJssx(shebaos[p].getBaseUp());//基数上限
				cbf.setGjbl(shebaos[p].getCompanyRatio());//公司比例
				cbf.setGsgdje(shebaos[p].getCompanyFixedMoney());//公司固定金额
				cbf.setGsqzgz(1);//公司取整规则  1:四舍五入
				cbf.setGrbl(shebaos[p].getPersonRatio());//个人比例
				cbf.setGrgdje(shebaos[p].getPersonFixedMoney());//个人固定金额
				cbf.setGrqzgz(1);//个人取整规则 1:四舍五入
				cbf.setType(1);//类型 1:社保 2:公积金
				cbf.setTjsjTime(new Date().getTime());//添加时间
				cbf.setTjryNum(userBean.getEmpNum());//添加人员
				cbf.setGssbhj(1);//公司社保合计 规则(四舍五入) 1:四舍五入
				cbf.setGrsbhj(1);//个人社保合计 规则(四舍五入) 1:四舍五入
				cbf.setGsgjjhj(1);//公司公积金合计 规则(四舍五入) 1:四舍五入
				cbf.setGrgjjhj(1);//个人公积金合计 规则(四舍五入) 1:四舍五入
				cbf.setCbfaid(insura.getId());//参保方案id 
				fzmxlist.add(cbf);
			}
		}
		if(fzmxlist.size()>0){
			sbgjjassocbfzmxmapper.insertsbgjjassocbfzmxList(fzmxlist);
		}
		
		
		List<SbgjjAssoCbfzmx> gjjlist=new ArrayList<SbgjjAssoCbfzmx>();
		SocialfundDto[] gongjijins = insura.getGongjijin();//
		if(gongjijins.length>0){
			for(int p=0;p<gongjijins.length;p++){
				SbgjjAssoCbfzmx cbf = new SbgjjAssoCbfzmx();
				cbf.setXz(gongjijins[p].getName());//险种
				cbf.setJsxx(gongjijins[p].getBaseDown());//基数下限
				cbf.setJssx(gongjijins[p].getBaseUp());//基数上限
				cbf.setGjbl(gongjijins[p].getCompanyRatio());//公司比例
				cbf.setGsgdje(gongjijins[p].getCompanyFixedMoney());//公司固定金额
				cbf.setGsqzgz(1);//公司取整规则  1:四舍五入
				cbf.setGrbl(gongjijins[p].getPersonRatio());//个人比例
				cbf.setGrgdje(gongjijins[p].getPersonFixedMoney());//个人固定金额
				cbf.setGrqzgz(1);//个人取整规则 1:四舍五入
				cbf.setType(2);//类型 1:社保 2:公积金
				cbf.setTjsjTime(new Date().getTime());//添加时间
				cbf.setTjryNum(userBean.getEmpNum());//添加人员
				cbf.setGssbhj(1);//公司社保合计 规则(四舍五入) 1:四舍五入
				cbf.setGrsbhj(1);//个人社保合计 规则(四舍五入) 1:四舍五入
				cbf.setGsgjjhj(1);//公司公积金合计 规则(四舍五入) 1:四舍五入
				cbf.setGrgjjhj(1);//个人公积金合计 规则(四舍五入) 1:四舍五入
				cbf.setCbfaid(insura.getId());//参保方案id 
				gjjlist.add(cbf);
			}
		}
		if(gjjlist.size()>0){
			sbgjjassocbfzmxmapper.insertsbgjjassocbfzmxList(gjjlist);
		}
		
		return new ResponseResult().success("成功", cbfa);
		
	}

	/**
	 *	参保人员
	 */
	@PostMapping(value = "/insonnel")
	@ApiOperation(value = "参保人员", httpMethod = "POST", notes = "接口发布说明")
	public ResponseResult Insuredpersonnel(@CurrentUser UserBean userBean,@RequestBody InspersonnelDto inspe) {
		
		int orgcode = userBean.getOrgCode();
		inspe.setOrgcode(orgcode);
		
		List<InsuredPersonnelDto> cbrylist = sbgjjadmincbrymapper.InsuredPersonnel(inspe);
		
		return new ResponseResult().success("成功", cbrylist);
	}
	
	
	/**
	 * 	参保员工号-根据 模糊 + 高級查詢-分页****************************************************************8
	 */
	@PostMapping(value = "/insured_employees")
	@ApiOperation(value = "参保员工号-根据 模糊 + 高級查詢-分页", httpMethod = "POST", notes = "接口发布说明")
	@ApiOperationSupport(order = 34)
	public Result<Object> InsuredEmployees(@CurrentUser UserBean userBean, @Validated @RequestBody InspersonnelDto inspe){
		
			IPage<InsuredPersonnelDto> page = new Page<InsuredPersonnelDto>(
					inspe.getCurrentPage() == null ? 1 : inspe.getCurrentPage(),
							inspe.getTotalPage() == null ? 10 : inspe.getTotalPage());
			inspe.setOrgcode(userBean.getOrgCode());
			
			IPage<InsuredPersonnelDto> pageAs = sbgjjadmincbrymapper.InsuredEmployees(page, inspe);
			List<InsuredPersonnelDto> listAs = pageAs.getRecords();
			return ResultUtil.data(pageAs, listAs, "操作成功!");
	}
	


	@GetMapping(value = "/insure/{name}")
	@ApiOperation(value = "投保事件", httpMethod = "GET", notes = "接口发布说明")
	public ResponseResult InsuranceIncident(@CurrentUser UserBean userBean,@PathVariable String name) {
		 
		
		List<SbgjjAssoCbfa> cbfalist = new LambdaQueryChainWrapper<SbgjjAssoCbfa>(sbgjjassocbfamapper).eq(SbgjjAssoCbfa::getCbcs, name)
				.eq(SbgjjAssoCbfa::getQyid, userBean.getOrgCode()).list();
		
		return new ResponseResult().success("成功", cbfalist);
	}

	
	/**
	 * 获取员工数据
	 */
	@GetMapping(value="/Employee")
	@ApiOperation(value = "获取员工数据-根据 组织机构代码", httpMethod = "GET", notes = "接口发布说明")
	@ApiOperationSupport(order = 26)
	public Result<List<YgglMainEmp>> getEmployeeData(@CurrentUser UserBean userBean) {
		
			int qyid = userBean.getOrgCode();//坏小孩【企业id】				
			
			List<YgglMainEmp> yggl = sbgjjadmincbrymapper.EmployeeListByorgCode(qyid);
			
			return ResultUtil.data(yggl);
			
	}

	@Autowired
	private SbgjjAssoDetailedMapper sbgjjassodetailedmapper;
	
	@PostMapping(value = "/insuplan")
	@ApiOperation(value = "投保", httpMethod = "POST", notes = "接口发布说明")
	public ResponseResult InsurancePlan(@CurrentUser UserBean userBean,@RequestBody InsureDto insuredto) {
		
		
		String[] usernums = insuredto.getUsernums();
		
		if(insuredto.getUsernum() > 0) {
			usernums = new String[1];
			usernums[0] = String.valueOf(insuredto.getUsernum());
		}
		
		
		if(usernums.length > 0){
			for(int f=0;f<usernums.length;f++){
				
//				Integer.valueOf(usernums[f])
				
				SbgjjAdminCbry cbry = SbgjjAdminCbry.builder().build();
				cbry.setUserNum(Integer.valueOf(usernums[f])); 
				if(insuredto.getSb()) {
					cbry.setSbcbState(2);//社保参保状态 0:未投保 1:正常在缴 2:暂不办理 3:已停保
					cbry.setSbjlStatime(null);//社保缴纳起始月
					cbry.setSbjljs(null);//社保缴纳基数
				}else {
					cbry.setSbcbState(1);//社保参保状态 0:未投保 1:正常在缴 2:暂不办理 3:已停保
					cbry.setSbjlStatime(insuredto.getJnmouth());//社保缴纳起始月
					cbry.setSbjljs(Double.valueOf(insuredto.getJnjs()));//社保缴纳基数
				}
				if(insuredto.getGjj()) {
					cbry.setGjjcbState(2);//公积金参保状态 0:未投保 1:正常在缴 2:暂不办理 3:已停保
					cbry.setGjjjlStatime(null);//公积金缴纳起始月
					cbry.setGjjjljs(null);//公积金缴纳基数
				}else {
					cbry.setGjjcbState(1);//公积金参保状态 0:未投保 1:正常在缴 2:暂不办理 3:已停保
					cbry.setGjjjlStatime(insuredto.getGjjJnmouth());//公积金缴纳起始月
					cbry.setGjjjljs(Double.valueOf(insuredto.getGjjJnjs()));//公积金缴纳基数
				}
				cbry.setJlMode(1);//缴纳方式 1:公司自缴
				cbry.setCbfacs(insuredto.getInsuredcities());//参保城市
				
				cbry.setSbjlEndtime(null);//社保缴纳结束月
				cbry.setGjjjlEndtime(null);//公积金缴纳结束月
				cbry.setInsureNum(userBean.getEmpNum());//投保人员
				cbry.setInsureTime(new Date().getTime());//投保时间
//				cbry.setStopNum();//停保人员
//				cbry.setStopTime();//停保时间
				cbry.setState(1);//状态 1:使用中;0:历史记录
				cbry.setQyid(userBean.getOrgCode());
				
				cbry.setSbfaid(insuredto.getSbfaid());
				cbry.setGjjfaid(insuredto.getGjjfaid());
				
				sbgjjadmincbrymapper.insertsbgjjadmincbry(cbry);
				int cbryid = cbry.getId();//参保人员
				
				List<SbgjjAssoDetailed> detalis = new ArrayList<SbgjjAssoDetailed>();
				
				//*************************************社保******************************************************
				double gssocial = 0;//公司社保总
				double grsocial = 0;//个人社保总
				if(!insuredto.getSb()) {
					List<SbgjjAssoCbfzmx> shebaolist = new LambdaQueryChainWrapper<SbgjjAssoCbfzmx>(sbgjjassocbfzmxmapper).eq(SbgjjAssoCbfzmx::getCbfaid, insuredto.getSbfaid())
							.eq(SbgjjAssoCbfzmx::getType,1).list();
				//社保******
					//公司
					
					SbgjjAssoDetailed.builder().build().delete(new QueryWrapper<SbgjjAssoDetailed>().lambda().eq(SbgjjAssoDetailed::getUserId, Integer.valueOf(usernums[f])).eq(SbgjjAssoDetailed::getOrgCode, userBean.getOrgCode())
							.eq(SbgjjAssoDetailed::getAttributionTime, insuredto.getJnmouth()));
					
					for(SbgjjAssoCbfzmx sbjs:shebaolist) {
						double jishuxx = 0;
						double preval = 0;
						if(sbjs.getGsgdje() > 0) {
							preval = sbjs.getGsgdje();
						}else {
							if(Double.valueOf(sbjs.getJsxx()) > Double.valueOf(insuredto.getJnjs())) {
								jishuxx = Double.valueOf(sbjs.getJsxx());//基数下限
							}else {
								jishuxx = Double.valueOf(insuredto.getJnjs());//基数下限
							}
					        BigDecimal gsbili = new BigDecimal(sbjs.getGjbl());//公司比例
							BigDecimal bfb = new BigDecimal(100);
							double gsh = gsbili.divide(bfb).doubleValue();
							preval = formatDouble1(Double.valueOf(jishuxx)*gsh);
						}
						
						SbgjjAssoDetailed deta = SbgjjAssoDetailed.builder().build();
						deta.setUserId(Integer.valueOf(usernums[f]));
						deta.setAttributionTime(insuredto.getJnmouth());//
						
						deta.setCbfzmxId(sbjs.getId());
						deta.setCoverageName(sbjs.getXz());
						deta.setPersonalAmount(0.0);
						deta.setCompanyAmount(preval);//公司金额
						deta.setXzType(1);//险种类型 1:社保;2:公积金
						deta.setOrgCode(userBean.getOrgCode());
						deta.setAddtime(new Date().getTime());
						detalis.add(deta);
						
						System.out.println("公司-社保:"+preval);
						gssocial += preval;
					}
					System.out.println("公司社保总:"+formatDouble1(gssocial));
					
					if(detalis.size()>0){
						sbgjjassodetailedmapper.insertsbgjjassodetailedList(detalis);
					}
					
					
					//个人
					
					for(SbgjjAssoCbfzmx sbjsgr:shebaolist) {
						double jishuxxgr = 0;
						double preval = 0;
						if(sbjsgr.getGrgdje() > 0) {
							preval = sbjsgr.getGrgdje();
						}else {
							if(Double.valueOf(sbjsgr.getJsxx()) > Double.valueOf(insuredto.getJnjs())) {
								jishuxxgr = Double.valueOf(sbjsgr.getJsxx());//基数下限
							}else {
								jishuxxgr = Double.valueOf(insuredto.getJnjs());//基数下限
							}
					        BigDecimal gsbigr = new BigDecimal(sbjsgr.getGrbl());//个人比例
							BigDecimal bfgr = new BigDecimal(100);
							double gsh = gsbigr.divide(bfgr).doubleValue();
							preval = formatDouble1(Double.valueOf(jishuxxgr)*gsh);
						}
						
						SbgjjAssoDetailed dtd = sbgjjassodetailedmapper.selectOne(new QueryWrapper<SbgjjAssoDetailed>().lambda().eq(SbgjjAssoDetailed::getUserId, Integer.valueOf(usernums[f]))
								.eq(SbgjjAssoDetailed::getAttributionTime, insuredto.getJnmouth()).eq(SbgjjAssoDetailed::getCbfzmxId, sbjsgr.getId()).eq(SbgjjAssoDetailed::getCoverageName, sbjsgr.getXz())
								.eq(SbgjjAssoDetailed::getOrgCode, userBean.getOrgCode()));
						if(dtd != null) {
							SbgjjAssoDetailed.builder().id(dtd.getId()).personalAmount(preval).build().updateById();
						}
						
						
						System.out.println("个人-社保:"+preval);
						grsocial += preval;
				        
					}
					System.out.println("个人社保总:"+formatDouble1(grsocial));
					
					if(!(insuredto.getJnmouth()).equals(insuredto.getGjjJnmouth())) {
//						sbgjjassoyjzdmapper.deletesbgjjassoyjzd(insuredto.getUsernum(),orgCode,insuredto.getJnmouth());
						
						SbgjjAssoYjzd yjzd = SbgjjAssoYjzd.builder().build();
						yjzd.setUserNum(Integer.valueOf(usernums[f]));//用户工号
						yjzd.setTotalmoney(String.valueOf(formatDouble1(formatDouble1(gssocial)+formatDouble1(grsocial))));//合计金额
						yjzd.setGsjltotal(String.valueOf(formatDouble1(gssocial)));//公司缴纳合计
						yjzd.setGrjltotal(String.valueOf(formatDouble1(grsocial)));//个人缴纳合计
						yjzd.setZdtype(1);//账单类型 1:公司自缴
						yjzd.setZymonth(insuredto.getJnmouth());//账单月份
						yjzd.setSbfaid(insuredto.getSbfaid());//社保参保方案id
						yjzd.setGjjfaid(insuredto.getGjjfaid());//公积金参保方案id
						yjzd.setQyid(userBean.getOrgCode());
						
						yjzd.setSbgshj(String.valueOf(formatDouble1(gssocial)));//社保公司合计
						yjzd.setSbgrhj(String.valueOf(formatDouble1(grsocial)));//社保个人合计
						yjzd.setSbhj(String.valueOf(formatDouble1(formatDouble1(gssocial)+formatDouble1(grsocial))));//社保合计
						yjzd.setGjjgshj("0.0");//公积金公司合计
						yjzd.setGjjgrhj("0.0");//公积金个人合计
						yjzd.setGjjhj("0.0");//公积金合计
						
						yjzd.setCbryid(cbryid);
						
						sbgjjassoyjzdmapper.insertsbgjjassoyjzd(yjzd);
					}
					
				}
				
				//*************************************公积金******************************************************
				double gsfund = 0;//公司公积金总
				double grgsfund = 0;//个人公积金总
				if(!insuredto.getGjj()) {
					List<SbgjjAssoCbfzmx> gongjjlist = new LambdaQueryChainWrapper<SbgjjAssoCbfzmx>(sbgjjassocbfzmxmapper).eq(SbgjjAssoCbfzmx::getCbfaid, insuredto.getGjjfaid())
							.eq(SbgjjAssoCbfzmx::getType,2).list();
				//公积金
					//公司
					
					for(SbgjjAssoCbfzmx gjjgs:gongjjlist) {
						double jishuxx = 0;
						double preval = 0;
						if(gjjgs.getGsgdje() > 0) {//公司固定金额
							preval = gjjgs.getGsgdje();
						}else {
							if(Double.valueOf(gjjgs.getJsxx()) > Double.valueOf(insuredto.getGjjJnjs())) {
								jishuxx = Double.valueOf(gjjgs.getJsxx());//基数下限
							}else {
								jishuxx = Double.valueOf(insuredto.getGjjJnjs());//基数下限
							}
							BigDecimal gsbili = new BigDecimal(gjjgs.getGjbl());//公司比例
							BigDecimal bfb = new BigDecimal(100);
							double gsh = gsbili.divide(bfb).doubleValue();
							preval = formatDouble1(Double.valueOf(jishuxx)*gsh);
						}
						
						
						SbgjjAssoDetailed deta = SbgjjAssoDetailed.builder().build();
						deta.setUserId(Integer.valueOf(usernums[f]));
						deta.setAttributionTime(insuredto.getJnmouth());//
						deta.setCbfzmxId(gjjgs.getId());
						deta.setCoverageName(gjjgs.getXz());
						deta.setPersonalAmount(0.0);
						deta.setCompanyAmount(preval);//公司金额
						deta.setXzType(2);//险种类型 1:社保;2:公积金
						deta.setOrgCode(userBean.getOrgCode());
						deta.setAddtime(new Date().getTime());
						deta.insert();
						
						System.out.println("公司-公积金:"+preval);
						gsfund += preval;
					}
					System.out.println("公司公积金总:"+formatDouble1(gsfund));
					
					//个人
					
					for(SbgjjAssoCbfzmx gjjgr:gongjjlist) {
						double jishuxxgr = 0;
						double preval = 0;
						if(gjjgr.getGrgdje() > 0) {
							preval = gjjgr.getGrgdje();
						}else {
							if(Double.valueOf(gjjgr.getJsxx()) > Double.valueOf(insuredto.getGjjJnjs())) {
								jishuxxgr = Double.valueOf(gjjgr.getJsxx());//基数下限
							}else {
								jishuxxgr = Double.valueOf(insuredto.getGjjJnjs());//基数下限
							}
					        BigDecimal gsbigr = new BigDecimal(gjjgr.getGrbl());//个人比例
							BigDecimal bfgr = new BigDecimal(100);
							double gsh = gsbigr.divide(bfgr).doubleValue();
							preval = formatDouble1(Double.valueOf(jishuxxgr)*gsh);
						}
						
						SbgjjAssoDetailed dtd = sbgjjassodetailedmapper.selectOne(new QueryWrapper<SbgjjAssoDetailed>().lambda().eq(SbgjjAssoDetailed::getUserId, Integer.valueOf(usernums[f]))
								.eq(SbgjjAssoDetailed::getAttributionTime, insuredto.getJnmouth()).eq(SbgjjAssoDetailed::getCbfzmxId, gjjgr.getId()).eq(SbgjjAssoDetailed::getCoverageName, gjjgr.getXz())
								.eq(SbgjjAssoDetailed::getOrgCode, userBean.getOrgCode()));
						if(dtd!=null) {
							SbgjjAssoDetailed.builder().id(dtd.getId()).personalAmount(preval).build().updateById();
						}
						
						System.out.println("个人-公积金:"+preval);
						grgsfund += preval;
					}
					System.out.println("个人公积金总:"+formatDouble1(grgsfund));
					
					if(!(insuredto.getJnmouth()).equals(insuredto.getGjjJnmouth())) {
//						sbgjjassoyjzdmapper.deletesbgjjassoyjzd(insuredto.getUsernum(),orgCode,insuredto.getGjjJnmouth());
						
						SbgjjAssoYjzd yjzd = SbgjjAssoYjzd.builder().build();
						yjzd.setUserNum(Integer.valueOf(usernums[f]));//用户工号
						yjzd.setTotalmoney(String.valueOf(formatDouble1(formatDouble1(gsfund)+formatDouble1(grgsfund))));//合计金额
						yjzd.setGsjltotal(String.valueOf(formatDouble1(gsfund)));//公司缴纳合计
						yjzd.setGrjltotal(String.valueOf(formatDouble1(grgsfund)));//个人缴纳合计
						yjzd.setZdtype(1);//账单类型 1:公司自缴
						yjzd.setZymonth(insuredto.getGjjJnmouth());//账单月份
						yjzd.setSbfaid(insuredto.getSbfaid());//社保参保方案id
						yjzd.setGjjfaid(insuredto.getGjjfaid());//公积金参保方案id
						yjzd.setQyid(userBean.getOrgCode());
						
						yjzd.setSbgshj("0.0");//社保公司合计
						yjzd.setSbgrhj("0.0");//社保个人合计
						yjzd.setSbhj("0.0");//社保合计
						yjzd.setGjjgshj(String.valueOf(formatDouble1(gsfund)));//公积金公司合计
						yjzd.setGjjgrhj(String.valueOf(formatDouble1(grgsfund)));//公积金个人合计
						yjzd.setGjjhj(String.valueOf(formatDouble1(formatDouble1(gsfund)+formatDouble1(grgsfund))));//公积金合计
						
						yjzd.setCbryid(cbryid);
						
						sbgjjassoyjzdmapper.insertsbgjjassoyjzd(yjzd);
					}
				}
				
				//合计金额       社保(个人+公司) +  公积金(个人+公司)
				//公司缴纳合计	社保(公司) +  公积金(公司)
				//个人缴纳合计	社保(个人) +  公积金(个人)
				
				if((insuredto.getJnmouth()).equals(insuredto.getGjjJnmouth())) {
					
//					sbgjjassoyjzdmapper.deletesbgjjassoyjzd(insuredto.getUsernum(),orgCode,insuredto.getGjjJnmouth());
					
					SbgjjAssoYjzd yjzd = SbgjjAssoYjzd.builder().build();
					yjzd.setUserNum(Integer.valueOf(usernums[f]));//用户工号
					
					yjzd.setTotalmoney(String.valueOf(formatDouble1((formatDouble1(gsfund)+formatDouble1(grgsfund)+formatDouble1(gssocial)+formatDouble1(grsocial)))));//合计金额
					
					yjzd.setGsjltotal(String.valueOf(formatDouble1(formatDouble1(gssocial)+formatDouble1(gsfund))));//公司缴纳合计
					yjzd.setGrjltotal(String.valueOf(formatDouble1(formatDouble1(grsocial)+formatDouble1(grgsfund))));//个人缴纳合计
					
					yjzd.setZdtype(1);//账单类型 1:公司自缴
					yjzd.setZymonth(insuredto.getJnmouth());//账单月份
					yjzd.setSbfaid(insuredto.getSbfaid());//社保参保方案id
					yjzd.setGjjfaid(insuredto.getGjjfaid());//公积金参保方案id
					yjzd.setQyid(userBean.getOrgCode());
					
					yjzd.setSbgshj(String.valueOf(formatDouble1(gssocial)));//社保公司合计
					yjzd.setSbgrhj(String.valueOf(formatDouble1(grsocial)));//社保个人合计
					yjzd.setSbhj(String.valueOf(formatDouble1(formatDouble1(gssocial)+formatDouble1(grsocial))));//社保合计
					
					yjzd.setGjjgshj(String.valueOf(formatDouble1(gsfund)));//公积金公司合计
					yjzd.setGjjgrhj(String.valueOf(formatDouble1(grgsfund)));//公积金个人合计
					yjzd.setGjjhj(String.valueOf(formatDouble1(formatDouble1(gsfund)+formatDouble1(grgsfund))));//公积金合计
					
					yjzd.setCbryid(cbryid);
					sbgjjassoyjzdmapper.insertsbgjjassoyjzd(yjzd);
				}
				
			}
		}
		
		return new ResponseResult().success("成功", null);
		
	}


	/**
     * 保留两位小数,四舍五入的一个老土的方法
     * @param d
     * @return
     */
    public static double formatDouble1(double d) {
        return (double)Math.round(d*100)/100;
    }


    @GetMapping(value = "/bjiiali/{id}")
	@ApiOperation(value = "停保社保公积金初始化", httpMethod = "GET", notes = "接口发布说明")
	public ResponseResult sbgjjInitialiplan(@PathVariable Integer id) {
		
		
		SbgjjAdminCbry cbry = new LambdaQueryChainWrapper<SbgjjAdminCbry>(sbgjjadmincbrymapper).eq(SbgjjAdminCbry::getState, 1).eq(SbgjjAdminCbry::getUserNum,id).one();
		
		return new ResponseResult().success("成功", cbry);
	}


    @PutMapping(value = "/stopimmed")
	@ApiOperation(value = "停保", httpMethod = "PUT", notes = "接口发布说明")
	public ResponseResult Stopimmediately(@CurrentUser UserBean userBean,@RequestBody StopimmediatelyDto stopi) {
		
		Integer empNum = userBean.getEmpNum();
		
		SbgjjAdminCbry cbry = SbgjjAdminCbry.builder().build();
		cbry.setId(stopi.getId());
		
		if(!stopi.getSb()) {//
			cbry.setSbjlEndtime(stopi.getSblastmouth());//社保缴纳结束月
			cbry.setSbcbState(3);
		}
		
		if(!stopi.getGjj()) {//
			cbry.setGjjjlEndtime(stopi.getGjjlastmouth());//公积金缴纳结束月
			cbry.setGjjcbState(3);
		}
		
		cbry.setStopNum(empNum);//停保人员
		cbry.setStopTime(new Date().getTime());//停保时间
		
		cbry.updateById();
		
		SbgjjAdminCbry istb = new LambdaQueryChainWrapper<SbgjjAdminCbry>(sbgjjadmincbrymapper).eq(SbgjjAdminCbry::getId,stopi.getId()).one();
		
		if(istb.getSbcbState() == 3 && istb.getGjjcbState() == 3) {//社保和公积金全部“停保”时
			SbgjjAdminCbry up = SbgjjAdminCbry.builder().build();
			up.setId(stopi.getId());
			up.setState(0);//状态 1:使用中;0:历史记录
			up.updateById();
		}
		
		return new ResponseResult().success("成功", cbry);
	}

	/**
	 *	调整--
	 */
    @PutMapping(value = "/adjustment")
	@ApiOperation(value = "调整", httpMethod = "PUT", notes = "接口发布说明")
	public ResponseResult adjustment(@CurrentUser UserBean userBean,@RequestBody InsureDto insuredto) {
		SbgjjAdminCbry cbry = SbgjjAdminCbry.builder().build();
		cbry.setUserNum(insuredto.getUsernum()); 
		if(insuredto.getSb()) {
			//cbry.setSbcbState(2);//社保参保状态 0:未投保 1:正常在缴 2:暂不办理 3:已停保
//			cbry.setSbjlStatime(null);//社保缴纳起始月
//			cbry.setSbjljs(null);//社保缴纳基数
		}else {
			cbry.setSbcbState(1);//社保参保状态 0:未投保 1:正常在缴 2:暂不办理 3:已停保
			cbry.setSbjlStatime(insuredto.getJnmouth());//社保缴纳起始月
			cbry.setSbjljs(Double.valueOf(insuredto.getJnjs()));//社保缴纳基数
		}
		if(insuredto.getGjj()) {
			//cbry.setGjjcbState(2);//公积金参保状态 0:未投保 1:正常在缴 2:暂不办理 3:已停保
//			cbry.setGjjjlStatime(null);//公积金缴纳起始月
//			cbry.setGjjjljs(null);//公积金缴纳基数
		}else {
			cbry.setGjjcbState(1);//公积金参保状态 0:未投保 1:正常在缴 2:暂不办理 3:已停保
			cbry.setGjjjlStatime(insuredto.getGjjJnmouth());//公积金缴纳起始月
			cbry.setGjjjljs(Double.valueOf(insuredto.getGjjJnjs()));//公积金缴纳基数
		}
		cbry.setCbfacs(insuredto.getInsuredcities());//参保城市
		
		cbry.setState(1);//状态 1:使用中;0:历史记录
		cbry.setQyid(userBean.getOrgCode());
		
		cbry.setSbfaid(insuredto.getSbfaid());
		cbry.setGjjfaid(insuredto.getGjjfaid());
		
		cbry.setId(insuredto.getId());
		cbry.updateById();
		
		
		//*************************************社保******************************************************
		double gssocial = 0;//公司社保总
		double grsocial = 0;//个人社保总
		if(!insuredto.getSb()) {
			List<SbgjjAssoCbfzmx> shebaolist = new LambdaQueryChainWrapper<SbgjjAssoCbfzmx>(sbgjjassocbfzmxmapper).eq(SbgjjAssoCbfzmx::getCbfaid, insuredto.getSbfaid())
					.eq(SbgjjAssoCbfzmx::getType,1).list();
		//社保******
			//公司
			
			for(SbgjjAssoCbfzmx sbjs:shebaolist) {
				double jishuxx = 0;
				double preval = 0;
				if(sbjs.getGsgdje() > 0) {
					preval = sbjs.getGsgdje();
				}else {
					if(Double.valueOf(sbjs.getJsxx()) > Double.valueOf(insuredto.getJnjs())) {
						jishuxx = Double.valueOf(sbjs.getJsxx());//基数下限
					}else {
						jishuxx = Double.valueOf(insuredto.getJnjs());//基数下限
					}
			        BigDecimal gsbili = new BigDecimal(sbjs.getGjbl());//公司比例
					BigDecimal bfb = new BigDecimal(100);
					double gsh = gsbili.divide(bfb).doubleValue();
					preval = formatDouble1(Double.valueOf(jishuxx)*gsh);
				}
				System.out.println(preval);
				gssocial += preval;
			}
			System.out.println("公司社保总:"+formatDouble1(gssocial));
			
			//个人
			
			for(SbgjjAssoCbfzmx sbjsgr:shebaolist) {
				double jishuxxgr = 0;
				double preval = 0;
				if(sbjsgr.getGrgdje() > 0) {
					preval = sbjsgr.getGrgdje();
				}else {
					if(Double.valueOf(sbjsgr.getJsxx()) > Double.valueOf(insuredto.getJnjs())) {
						jishuxxgr = Double.valueOf(sbjsgr.getJsxx());//基数下限
					}else {
						jishuxxgr = Double.valueOf(insuredto.getJnjs());//基数下限
					}
			        BigDecimal gsbigr = new BigDecimal(sbjsgr.getGrbl());//个人比例
					BigDecimal bfgr = new BigDecimal(100);
					double gsh = gsbigr.divide(bfgr).doubleValue();
					preval = formatDouble1(Double.valueOf(jishuxxgr)*gsh);
				}
				System.out.println(preval);
				grsocial += preval;
		        
			}
			System.out.println("个人社保总:"+formatDouble1(grsocial));
			
			String gjjgshj_ = "0.0";
			String gjjgrhj_ = "0.0";
			String gjjhj_ = "0.0";
			if(!(insuredto.getJnmouth()).equals(insuredto.getGjjJnmouth())) {
				
				sbgjjassoyjzdmapper.deletesbgjjassoyjzd(insuredto.getUsernum(),userBean.getOrgCode(),insuredto.getJnmouth(),insuredto.getId());
				
				SbgjjAssoYjzd yjzd = SbgjjAssoYjzd.builder().build();
				yjzd.setUserNum(insuredto.getUsernum());//用户工号
				yjzd.setTotalmoney(String.valueOf(formatDouble1(formatDouble1(gssocial)+formatDouble1(grsocial))));//合计金额
				yjzd.setGsjltotal(String.valueOf(formatDouble1(gssocial)));//公司缴纳合计
				yjzd.setGrjltotal(String.valueOf(formatDouble1(grsocial)));//个人缴纳合计
				yjzd.setZdtype(1);//账单类型 1:公司自缴
				yjzd.setZymonth(insuredto.getJnmouth());//账单月份
				yjzd.setSbfaid(insuredto.getSbfaid());//社保参保方案id
				yjzd.setGjjfaid(insuredto.getGjjfaid());//公积金参保方案id
				yjzd.setQyid(userBean.getOrgCode());
				
				yjzd.setSbgshj(String.valueOf(formatDouble1(gssocial)));//社保公司合计
				yjzd.setSbgrhj(String.valueOf(formatDouble1(grsocial)));//社保个人合计
				yjzd.setSbhj(String.valueOf(formatDouble1(formatDouble1(gssocial)+formatDouble1(grsocial))));//社保合计
				yjzd.setGjjgshj(gjjgshj_);//公积金公司合计
				yjzd.setGjjgrhj(gjjgrhj_);//公积金个人合计
				yjzd.setGjjhj(gjjhj_);//公积金合计
				
				yjzd.setCbryid(insuredto.getId());
				
				sbgjjassoyjzdmapper.insertsbgjjassoyjzd(yjzd);
			}
			
		}
		
		//*************************************公积金******************************************************
		double gsfund = 0;//公司公积金总
		double grgsfund = 0;//个人公积金总
		if(!insuredto.getGjj()) {
			List<SbgjjAssoCbfzmx> gongjjlist = new LambdaQueryChainWrapper<SbgjjAssoCbfzmx>(sbgjjassocbfzmxmapper).eq(SbgjjAssoCbfzmx::getCbfaid, insuredto.getGjjfaid())
					.eq(SbgjjAssoCbfzmx::getType,2).list();
		//公积金
			//公司
			
			for(SbgjjAssoCbfzmx gjjgs:gongjjlist) {
				double jishuxx = 0;
				double preval = 0;
				if(gjjgs.getGsgdje() > 0) {//公司固定金额
					preval = gjjgs.getGsgdje();
				}else {
					if(Double.valueOf(gjjgs.getJsxx()) > Double.valueOf(insuredto.getGjjJnjs())) {
						jishuxx = Double.valueOf(gjjgs.getJsxx());//基数下限
					}else {
						jishuxx = Double.valueOf(insuredto.getGjjJnjs());//基数下限
					}
					BigDecimal gsbili = new BigDecimal(gjjgs.getGjbl());//公司比例
					BigDecimal bfb = new BigDecimal(100);
					double gsh = gsbili.divide(bfb).doubleValue();
					preval = formatDouble1(Double.valueOf(jishuxx)*gsh);
				}
				System.out.println(preval);
				gsfund += preval;
			}
			System.out.println("公司公积金总:"+formatDouble1(gsfund));
			
			//个人
			
			for(SbgjjAssoCbfzmx gjjgr:gongjjlist) {
				double jishuxxgr = 0;
				double preval = 0;
				if(gjjgr.getGrgdje() > 0) {
					preval = gjjgr.getGrgdje();
				}else {
					if(Double.valueOf(gjjgr.getJsxx()) > Double.valueOf(insuredto.getGjjJnjs())) {
						jishuxxgr = Double.valueOf(gjjgr.getJsxx());//基数下限
					}else {
						jishuxxgr = Double.valueOf(insuredto.getGjjJnjs());//基数下限
					}
			        BigDecimal gsbigr = new BigDecimal(gjjgr.getGrbl());//个人比例
					BigDecimal bfgr = new BigDecimal(100);
					double gsh = gsbigr.divide(bfgr).doubleValue();
					preval = formatDouble1(Double.valueOf(jishuxxgr)*gsh);
				}
				System.out.println(preval);
				grgsfund += preval;
			}
			System.out.println("个人公积金总:"+formatDouble1(grgsfund));
			String sbgshj_="0.0";
			String sbgrhj_="0.0";
			String sbhj_="0.0";
			if(!(insuredto.getJnmouth()).equals(insuredto.getGjjJnmouth())) {
				
				sbgjjassoyjzdmapper.deletesbgjjassoyjzd(insuredto.getUsernum(),userBean.getOrgCode(),insuredto.getGjjJnmouth(),insuredto.getId());
				
				SbgjjAssoYjzd yjzd = SbgjjAssoYjzd.builder().build();
				yjzd.setUserNum(insuredto.getUsernum());//用户工号
				yjzd.setTotalmoney(String.valueOf(formatDouble1(formatDouble1(gsfund)+formatDouble1(grgsfund))));//合计金额
				yjzd.setGsjltotal(String.valueOf(formatDouble1(gsfund)));//公司缴纳合计
				yjzd.setGrjltotal(String.valueOf(formatDouble1(grgsfund)));//个人缴纳合计
				yjzd.setZdtype(1);//账单类型 1:公司自缴
				yjzd.setZymonth(insuredto.getGjjJnmouth());//账单月份
				yjzd.setSbfaid(insuredto.getSbfaid());//社保参保方案id
				yjzd.setGjjfaid(insuredto.getGjjfaid());//公积金参保方案id
				yjzd.setQyid(userBean.getOrgCode());
				
				yjzd.setSbgshj(sbgshj_);//社保公司合计
				yjzd.setSbgrhj(sbgrhj_);//社保个人合计
				yjzd.setSbhj(sbhj_);//社保合计
				yjzd.setGjjgshj(String.valueOf(formatDouble1(gsfund)));//公积金公司合计
				yjzd.setGjjgrhj(String.valueOf(formatDouble1(grgsfund)));//公积金个人合计
				yjzd.setGjjhj(String.valueOf(formatDouble1(formatDouble1(gsfund)+formatDouble1(grgsfund))));//公积金合计
				
				yjzd.setCbryid(insuredto.getId());
				sbgjjassoyjzdmapper.insertsbgjjassoyjzd(yjzd);
			}
		}
		
		//合计金额       社保(个人+公司) +  公积金(个人+公司)
		//公司缴纳合计	社保(公司) +  公积金(公司)
		//个人缴纳合计	社保(个人) +  公积金(个人)
		SbgjjAssoYjzd yjsb = new LambdaQueryChainWrapper<SbgjjAssoYjzd>(sbgjjassoyjzdmapper).eq(SbgjjAssoYjzd::getUserNum, insuredto.getUsernum())
				.eq(SbgjjAssoYjzd::getQyid,userBean.getOrgCode()).eq(SbgjjAssoYjzd::getZymonth,insuredto.getJnmouth()).eq(SbgjjAssoYjzd::getCbryid,insuredto.getId()).one();
		
		SbgjjAssoYjzd yjyg = new LambdaQueryChainWrapper<SbgjjAssoYjzd>(sbgjjassoyjzdmapper).eq(SbgjjAssoYjzd::getUserNum, insuredto.getUsernum())
		.eq(SbgjjAssoYjzd::getQyid,userBean.getOrgCode()).eq(SbgjjAssoYjzd::getZymonth,insuredto.getGjjJnmouth()).eq(SbgjjAssoYjzd::getCbryid,insuredto.getId()).one();
		
		if((insuredto.getJnmouth()).equals(insuredto.getGjjJnmouth())) {
			
			sbgjjassoyjzdmapper.deletesbgjjassoyjzd(insuredto.getUsernum(),userBean.getOrgCode(),insuredto.getGjjJnmouth(),insuredto.getId());
			
			SbgjjAssoYjzd yjzd = SbgjjAssoYjzd.builder().build();
			yjzd.setUserNum(insuredto.getUsernum());//用户工号
			
//			yjzd.setTotalmoney(String.valueOf(formatDouble1((formatDouble1(gsfund)+formatDouble1(grgsfund)+formatDouble1(gssocial)+formatDouble1(grsocial)))));//合计金额
//			
//			yjzd.setGsjltotal(String.valueOf(formatDouble1(formatDouble1(gssocial)+formatDouble1(gsfund))));//公司缴纳合计
//			yjzd.setGrjltotal(String.valueOf(formatDouble1(formatDouble1(grsocial)+formatDouble1(grgsfund))));//个人缴纳合计
			
			yjzd.setZdtype(1);//账单类型 1:公司自缴
			yjzd.setZymonth(insuredto.getJnmouth());//账单月份
			yjzd.setSbfaid(insuredto.getSbfaid());//社保参保方案id
			yjzd.setGjjfaid(insuredto.getGjjfaid());//公积金参保方案id
			yjzd.setQyid(userBean.getOrgCode());
			
			String sbgshj_="0.0";
			String sbgrhj_="0.0";
			String sbhj_="0.0";
			if(insuredto.getSb()) {//为true时 表示当前不用重新计算
				if(yjsb != null) {
					sbgshj_ = yjsb.getSbgshj();
					sbgrhj_ = yjsb.getSbgrhj();
					sbhj_ = yjsb.getSbhj();
				}
				yjzd.setSbgshj(sbgshj_);//社保公司合计
				yjzd.setSbgrhj(sbgrhj_);//社保个人合计
				yjzd.setSbhj(sbhj_);//社保合计	
			}else {
				yjzd.setSbgshj(String.valueOf(formatDouble1(gssocial)));//社保公司合计
				yjzd.setSbgrhj(String.valueOf(formatDouble1(grsocial)));//社保个人合计
				yjzd.setSbhj(String.valueOf(formatDouble1(formatDouble1(gssocial)+formatDouble1(grsocial))));//社保合计
			}
			
			
			String gjjgshj_ = "0.0";
			String gjjgrhj_ = "0.0";
			String gjjhj_ = "0.0";
			if(insuredto.getGjj()) {
				if(yjyg!= null) {
					gjjgshj_ = yjyg.getGjjgshj();
				    gjjgrhj_ = yjyg.getGjjgrhj();
				    gjjhj_ = yjyg.getGjjhj();
				}
			    yjzd.setGjjgshj(gjjgshj_);//公积金公司合计
				yjzd.setGjjgrhj(gjjgrhj_);//公积金个人合计
				yjzd.setGjjhj(gjjhj_);//公积金合计
			}else {
				yjzd.setGjjgshj(String.valueOf(formatDouble1(gsfund)));//公积金公司合计
				yjzd.setGjjgrhj(String.valueOf(formatDouble1(grgsfund)));//公积金个人合计
				yjzd.setGjjhj(String.valueOf(formatDouble1(formatDouble1(gsfund)+formatDouble1(grgsfund))));//公积金合计
			}
			
			//
			
			yjzd.setTotalmoney(String.valueOf(formatDouble1(formatDouble1(Double.valueOf(yjzd.getSbgshj()) + Double.valueOf(yjzd.getGjjgshj())) + formatDouble1(Double.valueOf(yjzd.getSbgrhj()) + Double.valueOf(yjzd.getGjjgrhj())))));//合计金额
			
			yjzd.setGsjltotal(String.valueOf(formatDouble1(Double.valueOf(yjzd.getSbgshj()) + Double.valueOf(yjzd.getGjjgshj()))));//公司缴纳合计
			yjzd.setGrjltotal(String.valueOf(formatDouble1(Double.valueOf(yjzd.getSbgrhj()) + Double.valueOf(yjzd.getGjjgrhj()))));//个人缴纳合计
			
			
			
			yjzd.setCbryid(insuredto.getId());
			sbgjjassoyjzdmapper.insertsbgjjassoyjzd(yjzd);
		}
		return new ResponseResult().success("成功", cbry);
		
//		return null;
	}

	/**
	 *	月结账
	 */
    @PostMapping(value = "/mochshe")
	@ApiOperation(value = "月结账单", httpMethod = "POST", notes = "接口发布说明")
	public ResponseResult Monthlycheckoutsheet(@CurrentUser UserBean userBean,@RequestBody MonthlyStatementDto monthly) {
		
		monthly.setOrgcode(userBean.getOrgCode());
		List<MonthlyCheckoutSheetDto> yjzlist = sbgjjassoyjzdmapper.MonthlycheckoutsheetList(monthly);
		
		return new ResponseResult().success("成功", yjzlist);
	}
}