package com.pj.project.tb_business; import java.math.BigDecimal; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.List; import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.pj.constants.UserTypeEnum; import com.pj.current.satoken.StpUserUtil; import com.pj.project.tb_business_item.TbBusinessItem; import com.pj.project.tb_business_item.TbBusinessItemService; import com.pj.project.tb_car.TbCar; import com.pj.project.tb_car.TbCarService; import com.pj.project.tb_costomer.TbCostomer; import com.pj.project.tb_costomer.TbCostomerService; import com.pj.project.tb_driver.TbDriver; import com.pj.project.tb_driver.TbDriverService; import com.pj.project.tb_item.TbItem; import com.pj.project.tb_item.TbItemService; import com.pj.project.tb_item_type.TbItemType; import com.pj.project.tb_item_type.TbItemTypeService; import com.pj.project4sp.admin.SpAdmin; import com.pj.project4sp.role.SpRoleUtil; import com.pj.project4sp.role4permission.SpRolePermissionService; import com.pj.utils.so.SoMap; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.pj.utils.sg.*; import javax.annotation.Resource; /** * Service: tb_business -- 入境登记 * * @author qzy */ @Service public class TbBusinessService extends ServiceImpl implements IService { /** * 底层 Mapper 对象 */ @Autowired TbBusinessMapper tbBusinessMapper; @Resource private TbCarService tbCarService; @Resource private TbDriverService tbDriverService; @Resource private TbCostomerService tbCostomerService; @Resource private TbItemService tbItemService; @Resource private TbItemTypeService tbItemTypeService; @Resource private TbBusinessItemService tbBusinessItemService; @Autowired SpRolePermissionService spRolePermissionService; /** * 增 */ public void addOrUpdate(TbBusiness t) { t.setCreateBy(StpUserUtil.getAdmin().getName()); createCar(t.getCardNo(), t.getCardSize(), t.getGoodsName()); TbDriver tbDriver = createDriver(t.getDriverIdCard(), t.getDriverName(), t.getDriverPhone()); t.setDriverId(tbDriver.getId()); String customerId = t.getCustomerId(); if (!StrUtil.equals(customerId, UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) { TbCostomer tbCostomer = tbCostomerService.getById(customerId); t.setCustomerName(tbCostomer.getName()); } else { t.setConfirmInputBy(StpUserUtil.getAdmin().getName()).setConfirmInput(1).setConfirmInputTime(new Date()); } if (StrUtil.isEmpty(t.getNo())) { t.setNo(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")) + RandomUtil.randomNumbers(4)); } List items = JSONUtil.toList(t.getItemJson(), TbBusinessItem.class); if (StrUtil.isEmpty(t.getId())) { t.setCreateTime(new Date()); } saveOrUpdate(t); tbBusinessItemService.removeByBusinessId(t.getId()); BigDecimal price = new BigDecimal(0); for (TbBusinessItem item : items) { Integer num = item.getNum(); TbItem tbItem = tbItemService.getById(item.getItemId()); price = price.add(NumberUtil.mul(num, tbItem.getPrice())); TbItemType tbItemType = tbItemTypeService.getById(tbItem.getTypeId()); item.setBusinessId(t.getId()).setItemCode(tbItem.getItemCode()) .setItemName(tbItem.getItemName()).setItemPrice(tbItem.getPrice()) .setItemTypeId(tbItem.getTypeId()).setItemTypeName(tbItemType.getName()) .setUnit(tbItem.getUnit()).setTotal(NumberUtil.mul(num, tbItem.getPrice())); } t.setItemPrice(price); this.saveOrUpdate(t); tbBusinessItemService.saveBatch(items); } private TbDriver createDriver(String idCardNo, String name, String phone) { TbDriver tbDriver = tbDriverService.findByIdCardNo(idCardNo); if (tbDriver == null) { tbDriver = new TbDriver(); tbDriver.setCreateTime(new Date()).setIdCard(idCardNo).setName(name).setPhone(phone); tbDriverService.save(tbDriver); } return tbDriver; } private TbCar createCar(String cardNo, String carSize, String goodsName) { TbCar tbCar = tbCarService.findByCardNo(cardNo); if (tbCar == null) { tbCar = new TbCar(); } tbCar.setCardNo(cardNo).setCardSize(carSize).setLastGoodsName(goodsName); tbCarService.saveOrUpdate(tbCar); return tbCar; } /** * 查集合 - 根据条件(参数为空时代表忽略指定条件) */ List getList(SoMap so) { return tbBusinessMapper.getList(so); } public void pay(String id, String payTicket) { TbBusiness tbBusiness = this.getById(id); SpAdmin admin = StpUserUtil.getAdmin(); tbBusiness.setPayStatus(2).setPayBy(admin.getName()) .setPayBy(StpUserUtil.getAdmin().getName()) .setPayTicket(payTicket) .setPayTime(new Date()); if (StrUtil.equals(admin.getCustomerId(), UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) { tbBusiness.setAdminConfirmPay(1).setPayStatus(3) .setAdminConfirmPayBy(admin.getName()).setAdminConfirmPayTime(new Date()); } this.updateById(tbBusiness); } public void confirm(List ids) { ids.forEach(id -> { TbBusiness business = this.getById(id); business.setConfirmInput(1).setConfirmInputTime(new Date()).setConfirmInputBy(StpUserUtil.getAdmin().getName()); this.updateById(business); }); } public void adminConfirmPay(List ids) { ids.forEach(id -> { TbBusiness business = this.getById(id); business.setAdminConfirmPayTime(new Date()).setAdminConfirmPay(1).setAdminConfirmPayBy(StpUserUtil.getAdmin().getName()) .setPayStatus(3); this.updateById(business); }); } public TbBusiness findCarTheNewRecord(String carNo) { QueryWrapper ew = new QueryWrapper<>(); ew.eq("card_no", carNo); ew.orderByDesc("id"); List list = this.list(ew); return list.isEmpty() ? null : list.get(0); } }