xuhy
2023-06-14 17f18c3d56a70327874aa7dda0adfa2f5e3dafce
management/guns-admin/src/main/java/com/stylefeng/guns/modular/system/controller/general/TBranchOfficeController.java
@@ -1,19 +1,24 @@
package com.stylefeng.guns.modular.system.controller.general;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.SuccessTip;
import com.stylefeng.guns.core.common.constant.state.ManagerStatus;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.SinataUtil;
import com.stylefeng.guns.modular.system.controller.resp.TBranchOfficeResp;
import com.stylefeng.guns.modular.system.controller.resp.TDriverResp;
import com.stylefeng.guns.modular.system.enums.StatusEnum;
import com.stylefeng.guns.modular.system.model.*;
import com.stylefeng.guns.modular.system.service.ITRegionService;
import com.stylefeng.guns.modular.system.service.ITSystemConfigService;
import com.stylefeng.guns.modular.system.service.*;
import com.stylefeng.guns.modular.system.util.RedisUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -23,8 +28,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.service.ITBranchOfficeService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
@@ -48,12 +54,21 @@
    @Autowired
    private ITRegionService tRegionService;
    @Autowired
    private ITDriverService tDriverService;
    @Autowired
    private ITDriverWorkService tDriverWorkService;
    @Autowired
    private IUserService userService;
    @Autowired
    private RedisUtil redisUtil;
    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
    public String index(Model model) {
        model.addAttribute("userType", Objects.requireNonNull(ShiroKit.getUser()).getRoleType());
        return PREFIX + "tBranchOffice.html";
    }
@@ -81,14 +96,14 @@
        TRegion district = tRegionService.selectOne(new EntityWrapper<TRegion>().eq("code", tBranchOffice.getDistrictCode())
                .last("LIMIT 1"));
        if(StringUtils.hasLength(tBranchOffice.getDistrictName())){
        if(StringUtils.hasLength(tBranchOffice.getDistrictName()) && !tBranchOffice.getDistrictName().equals(tBranchOffice.getCityName())){
            tBranchOfficeResp.setArea(tBranchOffice.getProvinceName()+"/"+tBranchOffice.getCityName()+"/"+tBranchOffice.getDistrictName());
        }else {
            tBranchOfficeResp.setArea(tBranchOffice.getProvinceName()+"/"+tBranchOffice.getCityName());
        }
        if(Objects.nonNull(city)){
            if(Objects.nonNull(district)){
            if(Objects.nonNull(district) && !district.getCode().equals(city.getCode())){
                tBranchOfficeResp.setAreaId(city.getParentId()+"/"+city.getId()+"/"+district.getId());
            }else {
                tBranchOfficeResp.setAreaId(city.getParentId()+"/"+city.getId());
@@ -121,15 +136,15 @@
        String[] split1 = areaId.split("/");
        List<TRegion> tRegions = tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", 0));
        model.addAttribute("provinceList",tRegions);
        List<Integer> provinceIds = tRegions.stream().map(TRegion::getId).collect(Collectors.toList());
        // 查询市
        List<TRegion> tRegions1 = tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", split1[0]));
        model.addAttribute("cityList",tRegions1);
        // 查询区
        List<Integer> cityIds = tRegions1.stream().map(TRegion::getId).collect(Collectors.toList());
        List<TRegion> tRegions2 = tRegionService.selectList(new EntityWrapper<TRegion>().in("parent_id", cityIds));
        model.addAttribute("districtList",tRegions2);
//        List<Integer> provinceIds = tRegions.stream().map(TRegion::getId).collect(Collectors.toList());
//        // 查询市
//        List<TRegion> tRegions1 = tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", split1[0]));
//        model.addAttribute("cityList",tRegions1);
//
//        // 查询区
//        List<Integer> cityIds = tRegions1.stream().map(TRegion::getId).collect(Collectors.toList());
//        List<TRegion> tRegions2 = tRegionService.selectList(new EntityWrapper<TRegion>().in("parent_id", cityIds));
//        model.addAttribute("districtList",tRegions2);
        if(StringUtils.hasLength(area) && StringUtils.hasLength(areaId)){
            String[] split = area.split("/");
@@ -148,13 +163,23 @@
            }else {
                model.addAttribute("districtId", "");
            }
            List<TRegion> tRegions1 = tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", split1[0]));
            model.addAttribute("cityList",tRegions1);
            // 查询区
            List<TRegion> tRegions2 = tRegionService.selectList(new EntityWrapper<TRegion>().in("parent_id", split1[1]));
            model.addAttribute("districtList",tRegions2);
        }else {
            model.addAttribute("cityList",new ArrayList<>());
            model.addAttribute("districtList",new ArrayList<>());
            model.addAttribute("provinceName","");
            model.addAttribute("cityName","split[1]");
            model.addAttribute("cityName","");
            model.addAttribute("districtName","");
            model.addAttribute("provinceId","");
            model.addAttribute("cityId","split1[1]");
            model.addAttribute("cityId","");
            model.addAttribute("districtId", "");
        }
        return PREFIX + "tBranchOfficeAreaAdd.html";
@@ -194,7 +219,7 @@
        // 查询区
        List<Integer> cityIds = tRegions1.stream().map(TRegion::getId).collect(Collectors.toList());
        List<TRegion> tRegions2 = tRegionService.selectList(new EntityWrapper<TRegion>().in("parent_id", cityIds));
        List<TRegion> tRegions2 = tRegionService.selectList(new EntityWrapper<TRegion>().in("parent_id", split1[1]));
        model.addAttribute("districtList",tRegions2);
        return PREFIX + "tBranchOfficeAreaUpdate.html";
@@ -207,6 +232,9 @@
    @ResponseBody
    public Object list(String branchOfficeName,String principal,String principalPhone,Integer operatingBusiness ,Integer status) {
        List<TBranchOfficeResp> tBranchOfficeRespList = tBranchOfficeService.getPageList(branchOfficeName,principal,principalPhone,operatingBusiness,status);
        for (TBranchOfficeResp tBranchOfficeResp : tBranchOfficeRespList) {
            tBranchOfficeResp.setUserType(Objects.requireNonNull(ShiroKit.getUser()).getRoleType());
        }
        // 分公司查询优惠券,订单,司机等信息
        tBranchOfficeService.queryOtherInfo(tBranchOfficeRespList);
        return tBranchOfficeRespList;
@@ -246,6 +274,23 @@
        TBranchOffice tBranchOffice = tBranchOfficeService.selectById(id);
        tBranchOffice.setStatus(StatusEnum.FREEZE.getCode());
        tBranchOfficeService.updateById(tBranchOffice);
        List<TDriver> list = tDriverService.selectList(new EntityWrapper<TDriver>()
                .eq("branchOfficeId", tBranchOffice.getId()));
        for (TDriver tDriver : list) {
            String value = redisUtil.getValue("DRIVER_" + tDriver.getPhone());
            redisUtil.remove(value);
            redisUtil.remove("DRIVER_" + tDriver.getPhone());
            TDriverWork tDriverWork = tDriverWorkService.selectOne(new EntityWrapper<TDriverWork>()
                    .eq("driverId", tDriver.getId())
                    .eq("status", 1)
                    .orderBy("workTime", false)
                    .last("LIMIT 1"));
            if(Objects.nonNull(tDriverWork)){
                tDriverWork.setStatus(2);
                tDriverWork.setOffWorkTime(new Date());
                tDriverWorkService.updateById(tDriverWork);
            }
        }
        return SUCCESS_TIP;
    }
@@ -263,10 +308,15 @@
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    public Object add(TBranchOffice tBranchOffice) {
        int count = tBranchOfficeService.selectCount(new EntityWrapper<TBranchOffice>().eq("branchOfficeName", tBranchOffice.getBranchOfficeName()));
        if(count>0){
            return new SuccessTip(500,"该分公司名称已存在!");
        }
        int count1 = userService.selectCount(new EntityWrapper<User>().eq("account", tBranchOffice.getAccount()));
        if (count1 > 0){
            return new SuccessTip(500,"该账号已存在!");
        }
        Object o = tBranchOfficeService.addOrUpdate(tBranchOffice);
@@ -277,6 +327,22 @@
        tBranchOffice.setStatus(StatusEnum.NORMAL.getCode());
        tBranchOfficeService.insert(tBranchOffice);
        //添加User对象
        User user = new User();
        user.setAccount(tBranchOffice.getAccount());
        user.setSalt(ShiroKit.getRandomSalt(5));
        user.setPassword(ShiroKit.md5(tBranchOffice.getPassword(), user.getSalt()));
        user.setStatus(ManagerStatus.OK.getCode());
        user.setRoleid("3");
        user.setDeptid(25);
        user.setCreatetime(new Date());
        user.setRoleType(2);
        user.setObjectId(tBranchOffice.getId());
        user.setName(tBranchOffice.getPrincipal());
        user.setSex(1);
        userService.insert(user);
        return SUCCESS_TIP;
    }
@@ -297,7 +363,43 @@
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    public Object update(TBranchOffice tBranchOffice) {
        TBranchOffice branchOffice = tBranchOfficeService.selectById(tBranchOffice.getId());
        User user = userService.selectOne(new EntityWrapper<User>()
                .eq("role_type", 2)
                .eq("object_id", branchOffice.getId())
                .last("LIMIT 1"));
        //判断账号是否已存在
        if (SinataUtil.isNotEmpty(tBranchOffice.getAccount()) && SinataUtil.isNotEmpty(tBranchOffice.getPassword())){
            if(Objects.isNull(user)){
                //添加User对象
                user.setAccount(tBranchOffice.getAccount());
                user.setSalt(ShiroKit.getRandomSalt(5));
                user.setPassword(ShiroKit.md5(tBranchOffice.getPassword(), user.getSalt()));
                user.setStatus(ManagerStatus.OK.getCode());
                user.setRoleid("3");
                user.setDeptid(25);
                user.setCreatetime(new Date());
                user.setRoleType(2);
                user.setObjectId(tBranchOffice.getId());
                user.setName(tBranchOffice.getPrincipal());
                user.setSex(1);
                userService.insert(user);
            }else {
                if (!tBranchOffice.getAccount().equals(user.getAccount())){
                    //判断账号是否已存在
                    int count = userService.selectCount(new EntityWrapper<User>().eq("account", tBranchOffice.getAccount()));
                    if (count > 0){
                        return new SuccessTip(500,"该账号已存在!");
                    }
                }
                user.setAccount(tBranchOffice.getAccount());
                user.setPassword(ShiroKit.md5(user.getPassword(), user.getSalt()));
                userService.updateById(user);
            }
        }
        TBranchOffice office = tBranchOfficeService.selectOne(new EntityWrapper<TBranchOffice>().eq("branchOfficeName", tBranchOffice.getBranchOfficeName())
                .last("LIMIT 1"));
        if(Objects.nonNull(office) && !tBranchOffice.getId().equals(office.getId())){