package com.ruoyi.warehouseBusiness.service.impl; import cn.hutool.core.collection.CollUtil; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.ruoyi.basicData.domain.TCorps; import com.ruoyi.basicData.domain.TFees; import com.ruoyi.basicData.domain.TGoods; import com.ruoyi.basicData.mapper.TCorpsMapper; import com.ruoyi.basicData.mapper.TFeesMapper; import com.ruoyi.basicData.mapper.TGoodsMapper; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.domain.entity.SysUser; import com.ruoyi.common.core.domain.model.LoginUser; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.system.mapper.SysDeptMapper; import com.ruoyi.warehouseBusiness.domain.BillnoDel; import com.ruoyi.warehouseBusiness.domain.TWarehouseAgreement; import com.ruoyi.warehouseBusiness.domain.TWarehouseAgreementitems; import com.ruoyi.warehouseBusiness.mapper.BillnoDelMapper; import com.ruoyi.warehouseBusiness.mapper.TWarehouseAgreementMapper; import com.ruoyi.warehouseBusiness.mapper.TWarehouseAgreementitemsMapper; import com.ruoyi.warehouseBusiness.service.ITWarehouseAgreementService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.util.*; /** * 仓储费Service业务层处理 * * @author ruoyi * @date 2020-12-11 */ @Service public class TWarehouseAgreementServiceImpl implements ITWarehouseAgreementService { @Autowired private TWarehouseAgreementMapper tWarehouseAgreementMapper; @Autowired private TWarehouseAgreementitemsMapper tWarehouseAgreementitemsMapper; @Autowired private BillnoSerialServiceImpl billnoSerialServiceImpl; @Autowired private BillnoDelMapper billnoDelMapper; @Autowired private TCorpsMapper tCorpsMapper; @Autowired private TGoodsMapper tGoodsMapper; @Autowired private TFeesMapper tFeesMapper; @Autowired private SysDeptMapper sysDeptMapper; /** * 查询仓储费 * * @param fId 仓储费ID * @return 仓储费 */ @Override public TWarehouseAgreement selectTWarehouseAgreementById(Long fId) { return tWarehouseAgreementMapper.selectTWarehouseAgreementById(fId); } @Override public Map selectTWarehouseAgreementById1(Long fId) { Map map = new HashMap<>(); // 查询协议主表数据 TWarehouseAgreement tWarehouseAgreement = tWarehouseAgreementMapper.selectTWarehouseAgreementById(fId); // 客户表 List corpsId = new ArrayList<>(); // 用户表 List sysUserList = new ArrayList<>(); // 商品 List goodsId = new ArrayList<>(); // 费用 List feesId = new ArrayList<>(); if (!StringUtils.isNull(tWarehouseAgreement)) { corpsId.add(tWarehouseAgreement.getfCorpid()); goodsId.add(tWarehouseAgreement.getfGoodsid()); map.put("tWarehouseAgreement", tWarehouseAgreement); } // 协议从表 TWarehouseAgreementitems tWarehouseAgreementitems = new TWarehouseAgreementitems(); tWarehouseAgreementitems.setfPid(fId); List tWarehouseAgreementitemss = tWarehouseAgreementitemsMapper.selectTWarehouseAgreementitemsList(tWarehouseAgreementitems); if (StringUtils.isNotEmpty(tWarehouseAgreementitemss)) { for (TWarehouseAgreementitems fees : tWarehouseAgreementitemss) { feesId.add(fees.getfFeeid()); } } List corpsList = new ArrayList<>(); List corpsIdList = StringUtils.integerDeduplication(corpsId); for (Long corpId : corpsIdList) { TCorps corps = tCorpsMapper.selectTCorpsById(corpId); if (StringUtils.isNotNull(corps)) { corpsList.add(corps); } } List goodsList = new ArrayList<>(); List goodsIdList = StringUtils.integerDeduplication(goodsId); for (Long goods : goodsIdList) { TGoods tGoods = tGoodsMapper.selectTGoodsById(goods); if (StringUtils.isNotNull(tGoods)) { goodsList.add(tGoods); } } List feesList = new ArrayList<>(); List longList = StringUtils.integerDeduplication(feesId); for (Long fees : longList) { TFees tFees = tFeesMapper.selectTFeesById(fees); if (StringUtils.isNotNull(tFees)) { feesList.add(tFees); } } map.put("corps", corpsList); map.put("goodsList", goodsList); map.put("feesList", feesList); map.put("tWarehouseAgreementitems", tWarehouseAgreementitemss); map.put("dept", sysDeptMapper.selectDeptById(tWarehouseAgreement.getfDeptid())); return map; } /** * 查询仓储费列表 * * @param tWarehouseAgreement 仓储费 * @return 仓储费 */ @Override public List> selectTWarehouseAgreementList1(TWarehouseAgreement tWarehouseAgreement) { return tWarehouseAgreementMapper.selectTWarehouseAgreementList1(tWarehouseAgreement); } /** * 查询仓储费列表 * * @param tWarehouseAgreement 仓储费 * @return 仓储费 */ @Override public List selectTWarehouseAgreementList(TWarehouseAgreement tWarehouseAgreement) { return tWarehouseAgreementMapper.selectTWarehouseAgreementList(tWarehouseAgreement); } /** * 新增 * @param agreement * @param agreementitems * @param loginUser * @return */ @Override public AjaxResult insertTWarehouseAgreement(String agreement, String agreementitems, LoginUser loginUser,int fFeetypeid) { Long fPid = null; TWarehouseAgreement tWarehouseAgreement = JSONArray.parseObject(agreement, TWarehouseAgreement.class); if(StringUtils.isNull(tWarehouseAgreement.getfId())){ // 如果是新数据 tWarehouseAgreement.setCreateBy(loginUser.getUser().getUserName()); // 业务编码 String billNo = billnoSerialServiceImpl.getBillNo("WA", new Date()); tWarehouseAgreement.setfBillno(billNo); tWarehouseAgreement.setCreateBy(loginUser.getUser().getUserName()); tWarehouseAgreement.setfDeptid(loginUser.getUser().getDeptId()); tWarehouseAgreement.setfFeetypeid((long) fFeetypeid); tWarehouseAgreementMapper.insertTWarehouseAgreement(tWarehouseAgreement); fPid = tWarehouseAgreement.getfId(); }else{ fPid = tWarehouseAgreement.getfId(); tWarehouseAgreement.setUpdateBy(loginUser.getUser().getUserName()); tWarehouseAgreement.setUpdateTime(new Date()); tWarehouseAgreementMapper.updateTWarehouseAgreement(tWarehouseAgreement); tWarehouseAgreementitemsMapper.deleteByFPid(fPid); } // 从表添加 if (StringUtils.isNotNull(agreementitems)) { JSONArray jsonDrArray = JSONArray.parseArray(agreementitems); List tWarehouseAgreementitemsList = JSONObject.parseArray(jsonDrArray.toJSONString(), TWarehouseAgreementitems.class); for (TWarehouseAgreementitems tWarehouseAgreementitems : tWarehouseAgreementitemsList) { tWarehouseAgreementitems.setfPid(fPid); tWarehouseAgreementitems.setCreateBy(loginUser.getUser().getUserName()); tWarehouseAgreementitems.setCreateTime(new Date()); tWarehouseAgreementitemsMapper.insertTWarehouseAgreementitems(tWarehouseAgreementitems); } } return AjaxResult.success(); } /** * 修改仓储费 * * @param tWarehouseAgreement 仓储费 * @return 结果 */ @Override public int updateTWarehouseAgreement(TWarehouseAgreement tWarehouseAgreement) { tWarehouseAgreement.setUpdateTime(DateUtils.getNowDate()); return tWarehouseAgreementMapper.updateTWarehouseAgreement(tWarehouseAgreement); } /** * 批量删除仓储费 * * @param fIds 需要删除的仓储费ID * @return 结果 */ @Override public int deleteTWarehouseAgreementByIds(Long[] fIds) { // 取出业务编号、 放入 billno_del for(Long id:fIds){ // 1、查询主表信息 TWarehouseAgreement tWarehouseAgreement = tWarehouseAgreementMapper.selectTWarehouseAgreementById(id); // 2、业务编号、客存编号 放入 billno_del BillnoDel billnoDel =new BillnoDel(); billnoDel.setBillType("WA"); billnoDel.setBillNo(tWarehouseAgreement.getfBillno()); billnoDelMapper.insertBillnoDel(billnoDel); } return tWarehouseAgreementMapper.deleteTWarehouseAgreementByIds(fIds); } /** * 删除仓储费信息 * * @param fId 仓储费ID * @return 结果 */ @Override public int deleteTWarehouseAgreementById(Long fId) { return tWarehouseAgreementMapper.deleteTWarehouseAgreementById(fId); } /** * 计算存储费用 * @author shanxin * @param fCorpid 出库客户Id * @param fGoodsid 物资类型Id * @param days 时长天数 * @param feeUnitid 计价单位 * @param itemNums 数量 * @return */ public Map getCarryingCost(Long fCorpid, Long fGoodsid, Long days, Long feeUnitid, Long itemNums) { Map map = new HashMap<>(); if (null == fCorpid || null == fGoodsid || null == days || null == feeUnitid || null == itemNums) { return null; } List itemList = this.tWarehouseAgreementitemsMapper.getItemsBytWarehouseAgreementMsg(fCorpid,fGoodsid,feeUnitid); if (CollUtil.isEmpty(itemList)) { return null; } BigDecimal money = new BigDecimal(0); Long dayLength = 0L; Long feeId = 0L; for (TWarehouseAgreementitems tWarehouseAgreementitems : itemList) { if (days < 1) break; dayLength = tWarehouseAgreementitems.getfEndays() - tWarehouseAgreementitems.getfFromdays() + 1L; if (days >= dayLength) { feeId = tWarehouseAgreementitems.getfFeeid(); money = money.add(this.getCalculate(itemNums,tWarehouseAgreementitems.getfPrice(),dayLength)); days -= dayLength; } else { feeId = tWarehouseAgreementitems.getfFeeid(); money = money.add(this.getCalculate(itemNums,feeUnitid,days)); } } map.put("amt", money); map.put("feeId", feeId); return map; } /** * 数量 * 单价 * 天数 * @param itemNums 数量 * @param unitPrice 单价 * @param dateLength 天数 * @return */ public BigDecimal getCalculate (Long itemNums,Long unitPrice,Long dateLength) { BigDecimal money = new BigDecimal(0); BigDecimal itemNumsBig = new BigDecimal(itemNums); BigDecimal unitPriceBig = new BigDecimal(unitPrice); BigDecimal bigDaysBig = new BigDecimal(dateLength); money = itemNumsBig.multiply(unitPriceBig).multiply(bigDaysBig); return money; } }