CompanyServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. package com.dk.oauth.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  4. import com.dk.common.exception.BaseBusinessException;
  5. import com.dk.common.infrastructure.enums.ErrorCodeEnum;
  6. import com.dk.common.model.response.mst.OrgResponse;
  7. import com.dk.common.model.response.mst.StaffResponse;
  8. import com.dk.common.model.vo.mst.OrgVO;
  9. import com.dk.common.model.vo.mst.StaffVO;
  10. import com.dk.common.response.ResponseCodeEnum;
  11. import com.dk.common.response.ResponseResultUtil;
  12. import com.dk.common.response.ResponseResultVO;
  13. import com.dk.common.util.AESUtil;
  14. import com.dk.oauth.convert.CompanyConvert;
  15. import com.dk.oauth.entity.*;
  16. import com.dk.oauth.feign.service.OrgFeign;
  17. import com.dk.oauth.feign.service.StaffFeign;
  18. import com.dk.oauth.mapper.*;
  19. import com.dk.oauth.query.*;
  20. import com.dk.oauth.response.*;
  21. import com.dk.oauth.service.ICompanyService;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.stereotype.Service;
  24. import org.springframework.transaction.annotation.Transactional;
  25. import org.springframework.web.context.request.RequestContextHolder;
  26. import org.springframework.web.context.request.ServletRequestAttributes;
  27. import javax.annotation.Resource;
  28. import javax.servlet.http.HttpServletRequest;
  29. import java.time.LocalDate;
  30. import java.util.ArrayList;
  31. import java.util.HashMap;
  32. import java.util.List;
  33. import java.util.Map;
  34. /**
  35. * 公司实现类
  36. *
  37. * @author admin
  38. * @since 2023-07-01 09:41:05
  39. */
  40. @Service("companyService")
  41. public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements ICompanyService {
  42. @Resource
  43. CompanyMapper companyMapper;
  44. @Resource
  45. UserMapper userMapper;
  46. @Resource
  47. StaffFeign staffFeign;
  48. @Resource
  49. OrgFeign orgFeign;
  50. @Resource
  51. DictionaryDataMapper dictionaryDataMapper;
  52. @Resource
  53. GoodsCategoryMapper goodsCategoryMapper;
  54. @Resource
  55. RoleMapper roleMapper;
  56. @Resource
  57. UnitMapper unitMapper;
  58. @Resource
  59. SaleChannelMapper saleChannelMapper;
  60. @Resource
  61. WarehouseMapper warehouseMapper;
  62. @Resource
  63. MoneyAccountMapper moneyAccountMapper;
  64. @Autowired
  65. private CompanyConvert companyConvert;
  66. /**
  67. * @desc : 通过ID查询
  68. * @author : admin
  69. * @date : 2023/2/3 13:32
  70. */
  71. @Override
  72. public ResponseResultVO selectById(Integer id) {
  73. return ResponseResultUtil.success(companyMapper.selectById(id));
  74. }
  75. /**
  76. * @desc : 可以绑定微信的员工人数 人数上限
  77. * @author : admin
  78. * @date : 2023/2/3 13:32
  79. */
  80. @Override
  81. public ResponseResultVO getCompanyMaxStaffNum(Integer id) {
  82. Company company = companyMapper.selectById(id);
  83. Map<String,Object> mp = new HashMap<>();
  84. // 解析license信息
  85. String licenseStr = AESUtil.desEncrypt(company.getLicense());
  86. if(licenseStr != null ){
  87. Map<String,Object> licenseMap = JSON.parseObject(licenseStr,Map.class);
  88. mp.put("web_max_num",Integer.parseInt(licenseMap.get("web_max_num").toString()));
  89. mp.put("wx_max_num",Integer.parseInt(licenseMap.get("wx_max_num").toString()));
  90. }else{
  91. mp.put("web_max_num",1);
  92. mp.put("wx_max_num",1);
  93. }
  94. return ResponseResultUtil.success(mp);
  95. }
  96. /**
  97. * @desc : 通过ID查询
  98. * @author : admin
  99. * @date : 2023/2/3 13:32
  100. */
  101. @Override
  102. public ResponseResultVO selectByOpenId(String openid) {
  103. return ResponseResultUtil.success(companyMapper.selectByOpenId(openid));
  104. }
  105. /**
  106. * @desc : 更新微信用的公司
  107. * @author : admin
  108. * @date : 2023/2/3 13:32
  109. */
  110. @Override
  111. @Transactional(rollbackFor = Exception.class)
  112. public ResponseResultVO updateWxUserCompany(Map<String, Object> map) {
  113. String userId = map.get("userId") + "";
  114. Integer cpId = Integer.valueOf(map.get("cpId") + "");
  115. List<Integer> cpIds = new ArrayList<>();
  116. cpIds.add(cpId);
  117. int i = userMapper.updateCpid(userId, cpId, cpIds);
  118. return ResponseResultUtil.success(true);
  119. }
  120. /**
  121. * @desc : 更新微信用的公司
  122. * @author : admin
  123. * @date : 2023/2/3 13:32
  124. */
  125. @Override
  126. @Transactional(rollbackFor = Exception.class)
  127. public ResponseResultVO updateCompanyCurStaffNum(Map<String, Object> map) {
  128. Integer curStaffNum = Integer.valueOf(map.get("curStaffNum") + "");
  129. Integer cpId = Integer.valueOf(map.get("cpId") + "");
  130. companyMapper.updateCompanyCurStaffNum(cpId, curStaffNum);
  131. return ResponseResultUtil.success(true);
  132. }
  133. /**
  134. * @desc : 选择公司更新微信用户的所在当前的公司
  135. * @author : admin
  136. * @date : 2023/2/3 13:32
  137. */
  138. @Override
  139. @Transactional(rollbackFor = Exception.class)
  140. public ResponseResultVO updateCurrentCpByWxid(Map<String, Object> map) {
  141. String userWxid = map.get("openid") + "";
  142. Integer cpId = null;
  143. if (map.get("cpId") != null) {
  144. cpId = Integer.valueOf(map.get("cpId") + "");
  145. }
  146. int i = userMapper.updateCurrentCpByWxid(userWxid, cpId);
  147. return ResponseResultUtil.success(true);
  148. }
  149. /**
  150. * @desc : 注册-商户
  151. * @author : 姜永辉
  152. * @date : 2024-02-20 13:55
  153. */
  154. @Override
  155. @Transactional(rollbackFor = Exception.class)
  156. public ResponseResultVO registerCompany(CompanyVO companyVO) {
  157. // 转实体
  158. Company company = companyConvert.convertToPo(companyVO);
  159. Company c = new Company();
  160. c.setFlgValid(true);
  161. c.setCpName(company.getCpName());
  162. Long aLong = companyMapper.countByCond(c);
  163. if (aLong > 0) {
  164. return ResponseResultUtil.error(ErrorCodeEnum.COMPANY_CODE_EXISTS.getCode(),
  165. ErrorCodeEnum.COMPANY_CODE_EXISTS.getMessage());
  166. }
  167. if ("PRO".equals(company.getGradeCode())){
  168. company.setCurStaffNum(100);
  169. company.setMaxStaffNum(100);
  170. company.setCpStatus("公司状态-待审");
  171. }else {
  172. company.setCurStaffNum(10);
  173. company.setMaxStaffNum(10);
  174. company.setCpStatus("公司状态-通过");
  175. }
  176. //获取具体服务的地址
  177. company.setSvcCode(companyMapper.selectServiceAllot(company.getGradeCode()));
  178. // 生成临时license授权
  179. Map<String,Object> licenseMap = new HashMap<>();
  180. licenseMap.put("grade_code",company.getGradeCode());
  181. licenseMap.put("end_date",company.getEndDate());
  182. licenseMap.put("web_max_num",companyVO.getWebMaxNum());
  183. licenseMap.put("wx_max_num",companyVO.getWxMaxNum());
  184. company.setLicenseSocial(AESUtil.aesEncrypt(JSON.toJSONString(licenseMap)) );
  185. companyMapper.insert(company);
  186. // 动态设置服务的地址
  187. ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
  188. .getRequestAttributes();
  189. HttpServletRequest request;
  190. if (attributes != null) {
  191. request = attributes.getRequest();
  192. request.setAttribute("svcCode", company.getSvcCode().replace("_", "-"));
  193. }
  194. // 组织机构 -顶级 和公司的名称一样
  195. // 二级 名称叫做 运营中心 返回的业务部门的id是运营中新的id
  196. OrgVO orgVO = new OrgVO();
  197. orgVO.setCpId(company.getCpId());
  198. orgVO.setOrgName(company.getCpName());
  199. // 二级 名称叫做 运营中心 返回的业务部门的id是运营中新的id
  200. ResponseResultVO<OrgResponse> orgResponseResponseResultVO = orgFeign.insertFeignOrg(orgVO);
  201. OrgResponse orgResponse = new OrgResponse();
  202. // 如果没有成功返回,状态设置为待审
  203. if (orgResponseResponseResultVO.getCode() != ResponseCodeEnum.SUCCESS.getCode()) {
  204. // 无用户
  205. throw new BaseBusinessException(ErrorCodeEnum.COMPANY_REGISTER_ERROR.getCode(),
  206. ErrorCodeEnum.COMPANY_REGISTER_ERROR.getMessage());
  207. } else {
  208. orgResponse = orgResponseResponseResultVO.getData();
  209. }
  210. // 生成员工
  211. UserLogin userLogin = userMapper.selectById(company.getOwner());
  212. StaffVO staffVO = new StaffVO();
  213. staffVO.setWxUserId(userLogin.getUserId());
  214. staffVO.setCpId(company.getCpId());
  215. staffVO.setStaffName(userLogin.getUserPhone());
  216. staffVO.setStaffPhone(userLogin.getUserPhone());
  217. staffVO.setOrgId(orgResponse.getOrgId());
  218. // 生成默认的角色数组--// 更新用户的角色ids--在插入基础数据的时候更新了。
  219. // staffVO.setRoleIds();
  220. // 注册商户时候插入员工
  221. ResponseResultVO<StaffResponse> staffResponseResponseResultVO = staffFeign.insertFeignStaff(staffVO);
  222. StaffResponse staffResponse = new StaffResponse();
  223. // 如果没有成功返回,状态设置为待审
  224. if (staffResponseResponseResultVO.getCode() != ResponseCodeEnum.SUCCESS.getCode()) {
  225. // 无用户
  226. throw new BaseBusinessException(staffResponseResponseResultVO.getCode(), staffResponseResponseResultVO.getMessage());
  227. } else {
  228. staffResponse = staffResponseResponseResultVO.getData();
  229. }
  230. // 更新 创建商户用户的公司id
  231. Integer cpId = company.getCpId();
  232. List<Integer> cpIds = new ArrayList<>();
  233. cpIds.add(cpId);
  234. // 当天的userid可能 存在加入公司
  235. if (userLogin.getJoinedCps()!=null && userLogin.getJoinedCps().size() > 0){
  236. cpIds.addAll(userLogin.getJoinedCps()) ;
  237. }
  238. userMapper.updateCpid(userLogin.getUserId(), cpId, cpIds);
  239. // 生成员工 角色 基础资料(仓库 渠道 收入 支出 入库 出库 账户 来源 盈亏原因) 计量单位 种类 打印票据 等等。
  240. String staffId = staffResponse.getStaffId();
  241. Map<String, List<Map<String, Object>>> map = new HashMap<>();
  242. List<Map<String, Object>> listCom = new ArrayList<>();
  243. Map<String, Object> m = new HashMap<>();
  244. m.put("gradeCode",company.getGradeCode());
  245. m.put("cpId",cpId);
  246. m.put("staffId",staffId);
  247. listCom.add(m);
  248. map.put("company",listCom);
  249. // 角色及权限
  250. List<RoleResponse> roleResponses = roleMapper.selectByCond(new RoleQuery());
  251. List<Map<String, Object>> listRoles = new ArrayList<>();
  252. for (int i = 0; i < roleResponses.size(); i++) {
  253. m = new HashMap<>();
  254. m.put("roleCode",roleResponses.get(i).getRoleCode());
  255. m.put("roleName",roleResponses.get(i).getRoleName());
  256. m.put("displayNo",roleResponses.get(i).getDisplayNo());
  257. listRoles.add(m);
  258. }
  259. map.put("roles",listRoles);
  260. // 基础资料(仓库 渠道 )
  261. List<WarehouseResponse> warehouseResponses = warehouseMapper.selectByCond(new WarehouseQuery());
  262. List<Map<String, Object>> listWarehouses = new ArrayList<>();
  263. for (int i = 0; i < warehouseResponses.size(); i++) {
  264. m = new HashMap<>();
  265. m.put("whCode",warehouseResponses.get(i).getWhCode());
  266. m.put("whName",warehouseResponses.get(i).getWhName());
  267. m.put("displayNo",warehouseResponses.get(i).getDisplayNo());
  268. m.put("flgDefault",warehouseResponses.get(i).getFlgDefault());
  269. listWarehouses.add(m);
  270. }
  271. map.put("warehouses",listWarehouses);
  272. // 基础资料(仓库 渠道 )
  273. List<SaleChannelResponse> saleChannelResponses = saleChannelMapper.selectByCond(new SaleChannelQuery());
  274. List<Map<String, Object>> listSaleChannels = new ArrayList<>();
  275. for (int i = 0; i < saleChannelResponses.size(); i++) {
  276. m = new HashMap<>();
  277. m.put("channelCode",saleChannelResponses.get(i).getChannelCode());
  278. m.put("channelName",saleChannelResponses.get(i).getChannelName());
  279. m.put("displayNo",saleChannelResponses.get(i).getDisplayNo());
  280. m.put("flgDefault",saleChannelResponses.get(i).getFlgDefault());
  281. listSaleChannels.add(m);
  282. }
  283. map.put("saleChannels",listSaleChannels);
  284. // 基础资料(仓库 渠道 收入 支出 入库 出库 账户 来源 盈亏原因)
  285. List<DictionaryDataResponse> dictionaryDataResponses = dictionaryDataMapper.selectByCond(new DictionaryDataQuery());
  286. List<Map<String, Object>> listDictionaryDatas = new ArrayList<>();
  287. for (int i = 0; i < dictionaryDataResponses.size(); i++) {
  288. m = new HashMap<>();
  289. m.put("dictCode",dictionaryDataResponses.get(i).getDictCode());
  290. m.put("dataCode",dictionaryDataResponses.get(i).getDataCode());
  291. m.put("dataValue",dictionaryDataResponses.get(i).getDataValue());
  292. m.put("displayNo",dictionaryDataResponses.get(i).getDisplayNo());
  293. m.put("flgDefault",dictionaryDataResponses.get(i).getFlgDefault());
  294. listDictionaryDatas.add(m);
  295. }
  296. map.put("dictionaryDatas",listDictionaryDatas);
  297. // 资金账户
  298. List<MoneyAccountResponse> moneyAccountResponses = moneyAccountMapper.selectByCond(new MoneyAccountQuery());
  299. List<Map<String, Object>> listMoneyAccounts = new ArrayList<>();
  300. for (int i = 0; i < moneyAccountResponses.size(); i++) {
  301. m = new HashMap<>();
  302. m.put("macCode",moneyAccountResponses.get(i).getMacCode());
  303. m.put("macName",moneyAccountResponses.get(i).getMacName());
  304. m.put("macType",moneyAccountResponses.get(i).getMacType());
  305. m.put("balance",moneyAccountResponses.get(i).getBalance());
  306. m.put("displayNo",moneyAccountResponses.get(i).getDisplayNo());
  307. m.put("flgDefault",moneyAccountResponses.get(i).getFlgDefault());
  308. listMoneyAccounts.add(m);
  309. }
  310. map.put("moneyAccounts",listMoneyAccounts);
  311. // 计量单位
  312. List<UnitResponse> unitResponses = unitMapper.selectByCond(new UnitQuery());
  313. List<Map<String, Object>> listUnits = new ArrayList<>();
  314. for (int i = 0; i < unitResponses.size(); i++) {
  315. m = new HashMap<>();
  316. m.put("unitCode",unitResponses.get(i).getUnitCode());
  317. m.put("unitName",unitResponses.get(i).getUnitName());
  318. m.put("decimalPlaces",unitResponses.get(i).getDecimalPlaces());
  319. m.put("displayNo",unitResponses.get(i).getDisplayNo());
  320. listUnits.add(m);
  321. }
  322. map.put("units",listUnits);
  323. // 种类
  324. List<GoodsCategoryResponse> goodsCategoryResponses = goodsCategoryMapper.selectByCond(new GoodsCategoryQuery());
  325. List<Map<String, Object>> listGoodsCategorys = new ArrayList<>();
  326. for (int i = 0; i < goodsCategoryResponses.size(); i++) {
  327. m = new HashMap<>();
  328. m.put("catCode",goodsCategoryResponses.get(i).getCatCode());
  329. m.put("catName",goodsCategoryResponses.get(i).getCatName());
  330. m.put("parentId",goodsCategoryResponses.get(i).getParentId());
  331. m.put("topId",goodsCategoryResponses.get(i).getTopId());
  332. m.put("levelUpper",goodsCategoryResponses.get(i).getLevelUpper());
  333. m.put("levelLower",goodsCategoryResponses.get(i).getLevelLower());
  334. m.put("levelLeaf",goodsCategoryResponses.get(i).getLevelLeaf());
  335. m.put("levelNo",goodsCategoryResponses.get(i).getLevelNo());
  336. m.put("levelCode",goodsCategoryResponses.get(i).getLevelCode());
  337. m.put("levelName",goodsCategoryResponses.get(i).getLevelName());
  338. m.put("displayNo",goodsCategoryResponses.get(i).getDisplayNo());
  339. listGoodsCategorys.add(m);
  340. }
  341. map.put("goodsCategorys",listGoodsCategorys);
  342. // 打印票据
  343. //List<PrintLayout> printLayoutResponses = printLayoutMapper.selectByCond(new PrintLayout());
  344. //List<Map<String, Object>> listPrintLayout = new ArrayList<>();
  345. //for (int i = 0; i < printLayoutResponses.size(); i++) {
  346. // m = new HashMap<>();
  347. // m.put("layoutName",printLayoutResponses.get(i).getLayoutName());
  348. // m.put("layoutData",printLayoutResponses.get(i).getLayoutData());
  349. // m.put("displayNo",printLayoutResponses.get(i).getDisplayNo());
  350. // m.put("docName",printLayoutResponses.get(i).getDocName());
  351. // m.put("cpId",cpId);
  352. // m.put("opCreateUserId",staffId);
  353. // listPrintLayout.add(m);
  354. //}
  355. //map.put("printLayout",listPrintLayout);
  356. ResponseResultVO listResponseResultVO = staffFeign.insertFeignCompanyData(map);
  357. // 如果没有成功返回
  358. if (listResponseResultVO.getCode() != ResponseCodeEnum.SUCCESS.getCode()) {
  359. // 无用户
  360. throw new BaseBusinessException(listResponseResultVO.getCode(), listResponseResultVO.getMessage());
  361. } else {
  362. // 更新用户的角色ids--在插入基础数据的时候更新了。
  363. // String rid = listResponseResultVO.getData();
  364. // roleids.add(rid);
  365. }
  366. return ResponseResultUtil.success(company);
  367. }
  368. /**
  369. * @desc : 续费
  370. * @author : 周兴
  371. * @date : 2024-05-17 13:55
  372. */
  373. @Transactional(rollbackFor = Exception.class)
  374. public ResponseResultVO reNewCompany(CompanyVO companyVO){
  375. // 转实体
  376. Company company = companyConvert.convertToPo(companyVO);
  377. // 生成临时license授权
  378. Map<String,Object> licenseMap = new HashMap<>();
  379. licenseMap.put("grade_code",company.getGradeCode());
  380. licenseMap.put("end_date",company.getEndDate());
  381. licenseMap.put("web_max_num",companyVO.getWebMaxNum());
  382. licenseMap.put("wx_max_num",companyVO.getWxMaxNum());
  383. company.setLicenseSocial(AESUtil.aesEncrypt(JSON.toJSONString(licenseMap)) );
  384. companyMapper.updateById(company);
  385. return ResponseResultUtil.success();
  386. }
  387. /**
  388. * @desc : 保存用户功能频率表
  389. * @author : 周兴
  390. * @date : 2024/3/28 9:39
  391. */
  392. @Transactional(
  393. rollbackFor = {Exception.class}
  394. )
  395. public ResponseResultVO<?> saveMenuFrequency(MenuFrequency menuFrequency) {
  396. MenuFrequency mf = companyMapper.selectMenuByUuid(menuFrequency);
  397. if(mf == null ){
  398. menuFrequency.setQty(1);
  399. }else{
  400. menuFrequency.setQty(mf.getQty() + 1);
  401. }
  402. // 保存用户功能频率表
  403. companyMapper.saveMenuFrequency(menuFrequency);
  404. return ResponseResultUtil.success();
  405. }
  406. }