| | |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.mapper.EntityWrapper; |
| | | import com.baomidou.mybatisplus.mapper.Wrapper; |
| | | import com.baomidou.mybatisplus.plugins.Page; |
| | | import com.stylefeng.guns.core.base.controller.BaseController; |
| | | import com.stylefeng.guns.core.base.tips.ErrorTip; |
| | |
| | | import org.springframework.web.bind.annotation.ResponseBody; |
| | | |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | /** |
| | | * 分公司管理控制器 |
| | |
| | | private ISpellOrderRuleService spellOrderRuleService; |
| | | @Autowired |
| | | private ShiroExtUtil shiroExtUtil; |
| | | |
| | | @Autowired |
| | | private ITDriverService tDriverService; |
| | | |
| | | @Autowired |
| | | private ITDriverServiceService tDriverServiceService; |
| | | |
| | | @Value("${pushMinistryOfTransport}") |
| | | private boolean pushMinistryOfTransport; |
| | |
| | | */ |
| | | @RequestMapping(value = "/update") |
| | | @ResponseBody |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Object update(TCompany tCompany,@RequestParam String subArr,String account,String password,Integer userId) { |
| | | String name = tCompany.getName(); |
| | | name = name.replaceAll("& lt;", "<"); |
| | |
| | | user.setPassword(ShiroKit.md5(password, user.getSalt())); |
| | | userService.updateById(user); |
| | | } |
| | | tCompany.setUpdateTime(new Date()); |
| | | tCompanyService.updateById(tCompany); |
| | | //添加经营区域 |
| | | addSocpe(subArr, tCompany.getId()); |
| | | |
| | | |
| | | // ========== 级联更新逻辑 ========== |
| | | TCompany oldCompany = tCompanyService.selectById(tCompany.getId()); |
| | | if (oldCompany != null && oldCompany.getType() == 2) { |
| | | // 1. 检测并级联更新经营业务 |
| | | cascadeUpdateBusinessScope(oldCompany, tCompany); |
| | | |
| | | // 2. 更新分公司本身 |
| | | tCompany.setUpdateTime(new Date()); |
| | | tCompanyService.updateById(tCompany); |
| | | addSocpe(subArr, tCompany.getId()); |
| | | } else { |
| | | tCompany.setUpdateTime(new Date()); |
| | | tCompanyService.updateById(tCompany); |
| | | addSocpe(subArr, tCompany.getId()); |
| | | } |
| | | // ========== 级联更新逻辑结束 ========== |
| | | |
| | | TCompany tCompany1 = tCompanyService.selectById(tCompany.getId()); |
| | | if (tCompany1.getType() == 1) { |
| | | new Thread(new Runnable() { |
| | |
| | | return SUCCESS_TIP; |
| | | } |
| | | |
| | | /** |
| | | * 级联更新经营业务 |
| | | * 当分公司取消某项经营业务时,同步更新下属加盟商和司机 |
| | | * @param oldCompany 更新前的分公司数据 |
| | | * @param newCompany 更新后的分公司数据 |
| | | */ |
| | | private void cascadeUpdateBusinessScope(TCompany oldCompany, TCompany newCompany) { |
| | | Integer companyId = oldCompany.getId(); |
| | | |
| | | // 1. 找出被取消的经营业务(从1变为2) |
| | | List<Integer> cancelledServiceTypes = new ArrayList<>(); |
| | | // 快车: isSpe -> type=1 |
| | | if (isServiceCancelled(oldCompany.getIsSpe(), newCompany.getIsSpe())) { |
| | | cancelledServiceTypes.add(1); |
| | | } |
| | | // 出租车: isTaxi -> type=2 |
| | | if (isServiceCancelled(oldCompany.getIsTaxi(), newCompany.getIsTaxi())) { |
| | | cancelledServiceTypes.add(2); |
| | | } |
| | | // 跨城: isCross -> type=3 |
| | | if (isServiceCancelled(oldCompany.getIsCross(), newCompany.getIsCross())) { |
| | | cancelledServiceTypes.add(3); |
| | | } |
| | | // 同城物流: isSameLogistics -> type=4 |
| | | if (isServiceCancelled(oldCompany.getIsSameLogistics(), newCompany.getIsSameLogistics())) { |
| | | cancelledServiceTypes.add(4); |
| | | } |
| | | // 跨城物流: isCrossLogistics -> type=5 |
| | | if (isServiceCancelled(oldCompany.getIsCrossLogistics(), newCompany.getIsCrossLogistics())) { |
| | | cancelledServiceTypes.add(5); |
| | | } |
| | | // 包车: isCharter -> type=6 |
| | | if (isServiceCancelled(oldCompany.getIsCharter(), newCompany.getIsCharter())) { |
| | | cancelledServiceTypes.add(6); |
| | | } |
| | | |
| | | if (cancelledServiceTypes.isEmpty()) { |
| | | return; // 没有取消任何业务,无需级联更新 |
| | | } |
| | | |
| | | // 2. 查询该分公司下的所有加盟商 |
| | | List<TCompany> franchiseeList = tCompanyService.selectList( |
| | | new EntityWrapper<TCompany>() |
| | | .eq("type", 3) |
| | | .eq("superiorId", companyId) |
| | | .ne("flag", "3") |
| | | ); |
| | | |
| | | // 3. 更新加盟商的经营业务字段 |
| | | for (TCompany franchisee : franchiseeList) { |
| | | updateFranchiseeBusinessScope(franchisee, cancelledServiceTypes); |
| | | } |
| | | |
| | | // 4. 删除分公司直属司机的对应经营业务记录 |
| | | deleteDriverServices(companyId, null, cancelledServiceTypes); |
| | | |
| | | // 5. 删除加盟商司机的对应经营业务记录 |
| | | List<Integer> franchiseeIds = franchiseeList.stream() |
| | | .map(TCompany::getId) |
| | | .collect(Collectors.toList()); |
| | | |
| | | if (!franchiseeIds.isEmpty()) { |
| | | for (Integer franchiseeId : franchiseeIds) { |
| | | deleteDriverServices(null, franchiseeId, cancelledServiceTypes); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 判断经营业务是否被取消(从1变为2) |
| | | * @param oldValue 旧值 |
| | | * @param newValue 新值 |
| | | * @return true表示被取消 |
| | | */ |
| | | private boolean isServiceCancelled(Integer oldValue, Integer newValue) { |
| | | return Integer.valueOf(1).equals(oldValue) && Integer.valueOf(2).equals(newValue); |
| | | } |
| | | |
| | | /** |
| | | * 更新加盟商的经营业务字段 |
| | | * @param franchisee 加盟商 |
| | | * @param cancelledTypes 被取消的业务类型列表 |
| | | */ |
| | | private void updateFranchiseeBusinessScope(TCompany franchisee, List<Integer> cancelledTypes) { |
| | | boolean needUpdate = false; |
| | | |
| | | // 快车 |
| | | if (cancelledTypes.contains(1) && Integer.valueOf(1).equals(franchisee.getIsSpe())) { |
| | | franchisee.setIsSpe(2); |
| | | needUpdate = true; |
| | | } |
| | | // 出租车 |
| | | if (cancelledTypes.contains(2) && Integer.valueOf(1).equals(franchisee.getIsTaxi())) { |
| | | franchisee.setIsTaxi(2); |
| | | needUpdate = true; |
| | | } |
| | | // 跨城 |
| | | if (cancelledTypes.contains(3) && Integer.valueOf(1).equals(franchisee.getIsCross())) { |
| | | franchisee.setIsCross(2); |
| | | needUpdate = true; |
| | | } |
| | | // 同城物流 |
| | | if (cancelledTypes.contains(4) && Integer.valueOf(1).equals(franchisee.getIsSameLogistics())) { |
| | | franchisee.setIsSameLogistics(2); |
| | | needUpdate = true; |
| | | } |
| | | // 跨城物流 |
| | | if (cancelledTypes.contains(5) && Integer.valueOf(1).equals(franchisee.getIsCrossLogistics())) { |
| | | franchisee.setIsCrossLogistics(2); |
| | | needUpdate = true; |
| | | } |
| | | // 包车 |
| | | if (cancelledTypes.contains(6) && Integer.valueOf(1).equals(franchisee.getIsCharter())) { |
| | | franchisee.setIsCharter(2); |
| | | needUpdate = true; |
| | | } |
| | | |
| | | if (needUpdate) { |
| | | franchisee.setUpdateTime(new Date()); |
| | | tCompanyService.updateById(franchisee); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 删除司机的经营业务记录 |
| | | * @param companyId 分公司ID(查询直属司机时使用) |
| | | * @param franchiseeId 加盟商ID(查询加盟商司机时使用) |
| | | * @param serviceTypes 要删除的业务类型列表 |
| | | */ |
| | | private void deleteDriverServices(Integer companyId, Integer franchiseeId, List<Integer> serviceTypes) { |
| | | Wrapper<TDriver> wrapper = new EntityWrapper<TDriver>().ne("flag", "3"); |
| | | |
| | | if (companyId != null) { |
| | | // 查询分公司直属司机(companyId匹配且franchiseeId为空或0) |
| | | wrapper.eq("companyId", companyId) |
| | | .andNew() |
| | | .isNull("franchiseeId") |
| | | .or() |
| | | .eq("franchiseeId", 0); |
| | | } else if (franchiseeId != null) { |
| | | // 查询加盟商司机 |
| | | wrapper.eq("franchiseeId", franchiseeId); |
| | | } else { |
| | | return; |
| | | } |
| | | |
| | | List<TDriver> drivers = tDriverService.selectList(wrapper); |
| | | |
| | | for (TDriver driver : drivers) { |
| | | tDriverServiceService.delete( |
| | | new EntityWrapper<TDriverService>() |
| | | .eq("driverId", driver.getId()) |
| | | .in("type", serviceTypes) |
| | | ); |
| | | } |
| | | } |
| | | |
| | | } |