|  |  |  | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.util.ObjectUtils; | 
|---|
|  |  |  | import org.springframework.web.context.request.RequestContextHolder; | 
|---|
|  |  |  | import org.springframework.web.context.request.ServletRequestAttributes; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.LinkedHashMap; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  | import java.util.Objects; | 
|---|
|  |  |  | import javax.servlet.http.HttpServletRequest; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONArray; | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
|---|
|  |  |  | import com.moral.api.entity.ResponsibilityUnit; | 
|---|
|  |  |  | import com.moral.api.entity.SysArea; | 
|---|
|  |  |  | import com.moral.api.mapper.ResponsibilityUnitMapper; | 
|---|
|  |  |  | import com.moral.api.pojo.redisBean.AccountInfoDTO; | 
|---|
|  |  |  | import com.moral.api.service.ResponsibilityUnitService; | 
|---|
|  |  |  | import com.moral.api.service.SysAreaService; | 
|---|
|  |  |  | import com.moral.constant.Constants; | 
|---|
|  |  |  | import com.moral.util.TokenUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Service | 
|---|
|  |  |  | public class ResponsibilityUnitServiceImpl extends ServiceImpl<ResponsibilityUnitMapper, ResponsibilityUnit> implements ResponsibilityUnitService { | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Integer insert(ResponsibilityUnit responsibilityUnit) { | 
|---|
|  |  |  | HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); | 
|---|
|  |  |  | String token = request.getHeader("token"); | 
|---|
|  |  |  | AccountInfoDTO accountInfoDTO = (AccountInfoDTO) TokenUtils.getUserInfoByToken(token); | 
|---|
|  |  |  | QueryWrapper<ResponsibilityUnit> wrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | wrapper.eq("unit_name",responsibilityUnit.getUnitName()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | if (ObjectUtils.isEmpty(units)){ | 
|---|
|  |  |  | responsibilityUnit.setIsDel(Constants.NOT_DELETE); | 
|---|
|  |  |  | responsibilityUnit.setCreateTime(new Date()); | 
|---|
|  |  |  | responsibilityUnit.setState(0); | 
|---|
|  |  |  | responsibilityUnit.setIsInvalid(0); | 
|---|
|  |  |  | responsibilityUnit.setCreateId(accountInfoDTO.getAccount().getId()); | 
|---|
|  |  |  | responsibilityUnit.setCreateName(accountInfoDTO.getAccount().getUserName()); | 
|---|
|  |  |  | responsibilityUnit.setCreateTime(new Date()); | 
|---|
|  |  |  | responsibilityUnit.setUpdateId(accountInfoDTO.getAccount().getId()); | 
|---|
|  |  |  | responsibilityUnit.setUpdateName(accountInfoDTO.getAccount().getUserName()); | 
|---|
|  |  |  | responsibilityUnit.setUpdateTime(new Date()); | 
|---|
|  |  |  | responsibilityUnit.setValue(JSONArray.toJSONString(responsibilityUnit.getParentCodeList())); | 
|---|
|  |  |  | responsibilityUnitMapper.insert(responsibilityUnit); | 
|---|
|  |  |  | return 200; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | public Map<String, Object> selectUnit(Map<String, Object> parameters) { | 
|---|
|  |  |  | QueryWrapper<ResponsibilityUnit> wrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | wrapper.eq("is_del",Constants.NOT_DELETE); | 
|---|
|  |  |  | //        wrapper.eq("is_invalid",0); | 
|---|
|  |  |  | int page = Integer.parseInt(parameters.get("page").toString()); | 
|---|
|  |  |  | int size = Integer.parseInt(parameters.get("size").toString()); | 
|---|
|  |  |  | Object parentName1 = parameters.get("parentCode"); | 
|---|
|  |  |  | Object areaName1 = parameters.get("areaCode"); | 
|---|
|  |  |  | //名称模糊查询 | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(parameters.get("name"))){ | 
|---|
|  |  |  | wrapper.like("unit_name",parameters.get("name").toString()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //上级区域查询 | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(parentName1)){ | 
|---|
|  |  |  | wrapper.eq("parent_code",Integer.parseInt(parentName1.toString())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //所属区域查询 | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(areaName1)){ | 
|---|
|  |  |  | wrapper.eq("parent_code",Integer.parseInt(areaName1.toString())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Page<ResponsibilityUnit> pageList = new Page<>(page, size); | 
|---|
|  |  |  | Page<ResponsibilityUnit> responsibilityUnitPage = responsibilityUnitMapper.selectPage(pageList, wrapper); | 
|---|
|  |  |  | for (ResponsibilityUnit record : responsibilityUnitPage.getRecords()) { | 
|---|
|  |  |  | Integer areaCode = record.getAreaCode(); | 
|---|
|  |  |  | SysArea areaName = sysAreaService.select(areaCode); | 
|---|
|  |  |  | SysArea sysArea= sysAreaService.select(areaCode); | 
|---|
|  |  |  | Integer parentCode = record.getParentCode(); | 
|---|
|  |  |  | SysArea parentName = sysAreaService.select(parentCode); | 
|---|
|  |  |  | if (ObjectUtils.isEmpty(areaName) || ObjectUtils.isEmpty(parentName)){ | 
|---|
|  |  |  | SysArea parentArea = sysAreaService.select(parentCode); | 
|---|
|  |  |  | if (ObjectUtils.isEmpty(sysArea) || ObjectUtils.isEmpty(parentArea)){ | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | record.setAreaName(areaName.getAreaName()); | 
|---|
|  |  |  | record.setParentName(parentName.getAreaName()); | 
|---|
|  |  |  | record.setAreaName(sysArea.getAreaName()); | 
|---|
|  |  |  | record.setParentName(parentArea.getAreaName()); | 
|---|
|  |  |  | record.setParentCodeList(JSONObject.parseObject(record.getValue(),List.class)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Map<String, Object> result = new LinkedHashMap<>(); | 
|---|
|  |  |  | result.put("total", pageList.getTotal()); | 
|---|
|  |  |  | 
|---|
|  |  |  | public Integer updateUnit(ResponsibilityUnit responsibilityUnit) { | 
|---|
|  |  |  | QueryWrapper<ResponsibilityUnit> wrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | wrapper.eq("unit_name",responsibilityUnit.getUnitName()); | 
|---|
|  |  |  | List<ResponsibilityUnit> units = responsibilityUnitMapper.selectList(wrapper); | 
|---|
|  |  |  | if (ObjectUtils.isEmpty(units)){ | 
|---|
|  |  |  | responsibilityUnitMapper.updateById(responsibilityUnit); | 
|---|
|  |  |  | return 200; | 
|---|
|  |  |  | wrapper.eq("area_code",responsibilityUnit.getAreaCode()); | 
|---|
|  |  |  | ResponsibilityUnit responsibilityUnit2 = responsibilityUnitMapper.selectOne(wrapper); | 
|---|
|  |  |  | if (ObjectUtils.isEmpty(responsibilityUnit2)){ | 
|---|
|  |  |  | AccountInfoDTO account = getAccount(); | 
|---|
|  |  |  | responsibilityUnit.setUpdateId(account.getAccount().getId()); | 
|---|
|  |  |  | responsibilityUnit.setUpdateName(account.getAccount().getUserName()); | 
|---|
|  |  |  | responsibilityUnit.setUpdateTime(new Date()); | 
|---|
|  |  |  | responsibilityUnit.setValue(JSONArray.toJSONString(responsibilityUnit.getParentCodeList())); | 
|---|
|  |  |  | //                responsibilityUnit.setParentCodeList(JSONObject.parseObject(responsibilityUnit.getValue(),List.class)); | 
|---|
|  |  |  | responsibilityUnitMapper.updateById(responsibilityUnit); | 
|---|
|  |  |  | return 200; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return -1; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | * | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void updateState(Integer id) { | 
|---|
|  |  |  | ResponsibilityUnit responsibilityUnit = responsibilityUnitMapper.selectById(id); | 
|---|
|  |  |  | responsibilityUnit.setState(1); | 
|---|
|  |  |  | public void updateState(Integer unitId) { | 
|---|
|  |  |  | ResponsibilityUnit responsibilityUnit = responsibilityUnitMapper.selectById(unitId); | 
|---|
|  |  |  | responsibilityUnit.setState(Constants.NOT_DELETE); | 
|---|
|  |  |  | AccountInfoDTO account = getAccount(); | 
|---|
|  |  |  | responsibilityUnit.setUpdateId(account.getAccount().getId()); | 
|---|
|  |  |  | responsibilityUnit.setUpdateName(account.getAccount().getUserName()); | 
|---|
|  |  |  | responsibilityUnit.setUpdateTime(new Date()); | 
|---|
|  |  |  | responsibilityUnitMapper.updateById(responsibilityUnit); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 是否作废 | 
|---|
|  |  |  | * @param id | 
|---|
|  |  |  | * @param unitId | 
|---|
|  |  |  | * @param code | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void updateInvalid(Integer id, String code) { | 
|---|
|  |  |  | ResponsibilityUnit responsibilityUnit = responsibilityUnitMapper.selectById(id); | 
|---|
|  |  |  | public void updateInvalid(Integer unitId, String code) { | 
|---|
|  |  |  | ResponsibilityUnit responsibilityUnit = responsibilityUnitMapper.selectById(unitId); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | responsibilityUnit.setIsInvalid(1); | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(code)){ | 
|---|
|  |  |  | responsibilityUnit.setInvalidReason(code); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | AccountInfoDTO account = getAccount(); | 
|---|
|  |  |  | responsibilityUnit.setUpdateId(account.getAccount().getId()); | 
|---|
|  |  |  | responsibilityUnit.setUpdateName(account.getAccount().getUserName()); | 
|---|
|  |  |  | responsibilityUnit.setUpdateTime(new Date()); | 
|---|
|  |  |  | responsibilityUnitMapper.updateById(responsibilityUnit); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取用户信息 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private AccountInfoDTO getAccount(){ | 
|---|
|  |  |  | HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); | 
|---|
|  |  |  | String token = request.getHeader("token"); | 
|---|
|  |  |  | AccountInfoDTO accountInfoDTO = (AccountInfoDTO) TokenUtils.getUserInfoByToken(token); | 
|---|
|  |  |  | return accountInfoDTO; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|