package com.dk.oauth.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.dk.common.exception.BaseBusinessException; import com.dk.common.infrastructure.enums.ErrorCodeEnum; import com.dk.common.model.response.mst.OrgResponse; import com.dk.common.model.response.mst.StaffResponse; import com.dk.common.model.vo.mst.OrgVO; import com.dk.common.model.vo.mst.StaffVO; import com.dk.common.response.ResponseCodeEnum; import com.dk.common.response.ResponseResultUtil; import com.dk.common.response.ResponseResultVO; import com.dk.oauth.entity.Company; import com.dk.oauth.entity.MenuFrequency; import com.dk.oauth.entity.UserLogin; import com.dk.oauth.feign.service.OrgFeign; import com.dk.oauth.feign.service.StaffFeign; import com.dk.oauth.mapper.*; import com.dk.oauth.query.*; import com.dk.oauth.response.*; import com.dk.oauth.service.ICompanyService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 公司实现类 * * @author admin * @since 2023-07-01 09:41:05 */ @Service("companyService") public class CompanyServiceImpl extends ServiceImpl implements ICompanyService { @Resource CompanyMapper companyMapper; @Resource UserMapper userMapper; @Resource StaffFeign staffFeign; @Resource OrgFeign orgFeign; @Resource DictionaryDataMapper dictionaryDataMapper; @Resource GoodsCategoryMapper goodsCategoryMapper; @Resource RoleMapper roleMapper; @Resource UnitMapper unitMapper; @Resource SaleChannelMapper saleChannelMapper; @Resource WarehouseMapper warehouseMapper; /** * @desc : 通过ID查询 * @author : admin * @date : 2023/2/3 13:32 */ @Override public ResponseResultVO selectById(Integer id) { return ResponseResultUtil.success(companyMapper.selectById(id)); } /** * @desc : 通过ID查询 * @author : admin * @date : 2023/2/3 13:32 */ @Override public ResponseResultVO selectByOpenId(String openid) { return ResponseResultUtil.success(companyMapper.selectByOpenId(openid)); } /** * @desc : 更新微信用的公司 * @author : admin * @date : 2023/2/3 13:32 */ @Override @Transactional(rollbackFor = Exception.class) public ResponseResultVO updateWxUserCompany(Map map) { String userId = map.get("userId") + ""; Integer cpId = Integer.valueOf(map.get("cpId") + ""); List cpIds = new ArrayList<>(); cpIds.add(cpId); int i = userMapper.updateCpid(userId, cpId, cpIds); return ResponseResultUtil.success(true); } /** * @desc : 选择公司更新微信用户的所在当前的公司 * @author : admin * @date : 2023/2/3 13:32 */ @Override @Transactional(rollbackFor = Exception.class) public ResponseResultVO updateCurrentCpByWxid(Map map) { String userWxid = map.get("openid") + ""; Integer cpId = null; if (map.get("cpId") != null) { cpId = Integer.valueOf(map.get("cpId") + ""); } int i = userMapper.updateCurrentCpByWxid(userWxid, cpId); return ResponseResultUtil.success(true); } /** * @desc : 注册-商户 * @author : 姜永辉 * @date : 2024-02-20 13:55 */ @Override @Transactional(rollbackFor = Exception.class) public ResponseResultVO registerCompany(Company company) { Company c = new Company(); c.setFlgValid(true); c.setCpName(company.getCpName()); Long aLong = companyMapper.countByCond(c); if (aLong > 0) { return ResponseResultUtil.error(ErrorCodeEnum.COMPANY_CODE_EXISTS.getCode(), ErrorCodeEnum.COMPANY_CODE_EXISTS.getMessage()); } if ("PRO".equals(company.getGradeCode())){ company.setCurStaffNum(100); company.setMaxStaffNum(100); company.setCpStatus("公司状态-待审"); }else { company.setCurStaffNum(10); company.setMaxStaffNum(10); company.setCpStatus("公司状态-通过"); } //获取具体服务的地址 company.setSvcCode(companyMapper.selectServiceAllot(company.getGradeCode())); companyMapper.insert(company); // 动态设置服务的地址 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder .getRequestAttributes(); HttpServletRequest request; if (attributes != null) { request = attributes.getRequest(); request.setAttribute("svcCode", company.getSvcCode().replace("_", "-")); } // 组织机构 -顶级 和公司的名称一样 // 二级 名称叫做 运营中心 返回的业务部门的id是运营中新的id OrgVO orgVO = new OrgVO(); orgVO.setCpId(company.getCpId()); orgVO.setOrgName(company.getCpName()); // 二级 名称叫做 运营中心 返回的业务部门的id是运营中新的id ResponseResultVO orgResponseResponseResultVO = orgFeign.insertFeignOrg(orgVO); OrgResponse orgResponse = new OrgResponse(); // 如果没有成功返回,状态设置为待审 if (orgResponseResponseResultVO.getCode() != ResponseCodeEnum.SUCCESS.getCode()) { // 无用户 throw new BaseBusinessException(ErrorCodeEnum.COMPANY_REGISTER_ERROR.getCode(), ErrorCodeEnum.COMPANY_REGISTER_ERROR.getMessage()); } else { orgResponse = orgResponseResponseResultVO.getData(); } // 生成员工 UserLogin userLogin = userMapper.selectById(company.getOwner()); StaffVO staffVO = new StaffVO(); staffVO.setWxUserId(userLogin.getUserId()); staffVO.setCpId(company.getCpId()); staffVO.setStaffName(userLogin.getUserPhone()); staffVO.setStaffPhone(userLogin.getUserPhone()); staffVO.setOrgId(orgResponse.getOrgId()); // 生成默认的角色数组--// 更新用户的角色ids--在插入基础数据的时候更新了。 // staffVO.setRoleIds(); // 注册商户时候插入员工 ResponseResultVO staffResponseResponseResultVO = staffFeign.insertFeignStaff(staffVO); StaffResponse staffResponse = new StaffResponse(); // 如果没有成功返回,状态设置为待审 if (staffResponseResponseResultVO.getCode() != ResponseCodeEnum.SUCCESS.getCode()) { // 无用户 throw new BaseBusinessException(staffResponseResponseResultVO.getCode(), staffResponseResponseResultVO.getMessage()); } else { staffResponse = staffResponseResponseResultVO.getData(); } // 更新 创建商户用户的公司id Integer cpId = company.getCpId(); List cpIds = new ArrayList<>(); cpIds.add(cpId); // 当天的userid可能 存在加入公司 if (userLogin.getJoinedCps()!=null && userLogin.getJoinedCps().size() > 0){ cpIds.addAll(userLogin.getJoinedCps()) ; } userMapper.updateCpid(userLogin.getUserId(), cpId, cpIds); // 生成员工 角色 基础资料(仓库 渠道 收入 支出 入库 出库 账户 来源 盈亏原因) 计量单位 种类 打印票据 等等。 String staffId = staffResponse.getStaffId(); Map>> map = new HashMap<>(); List> listCom = new ArrayList<>(); Map m = new HashMap<>(); m.put("gradeCode",company.getGradeCode()); m.put("cpId",cpId); m.put("staffId",staffId); listCom.add(m); map.put("company",listCom); // 角色及权限 List roleResponses = roleMapper.selectByCond(new RoleQuery()); List> listRoles = new ArrayList<>(); for (int i = 0; i < roleResponses.size(); i++) { m = new HashMap<>(); m.put("roleCode",roleResponses.get(i).getRoleCode()); m.put("roleName",roleResponses.get(i).getRoleName()); m.put("displayNo",roleResponses.get(i).getDisplayNo()); m.put("cpId",cpId); m.put("opCreateUserId",staffId); listRoles.add(m); } map.put("roles",listRoles); // 基础资料(仓库 渠道 ) List warehouseResponses = warehouseMapper.selectByCond(new WarehouseQuery()); List> listWarehouses = new ArrayList<>(); for (int i = 0; i < warehouseResponses.size(); i++) { m = new HashMap<>(); m.put("whCode",warehouseResponses.get(i).getWhCode()); m.put("whName",warehouseResponses.get(i).getWhName()); m.put("displayNo",warehouseResponses.get(i).getDisplayNo()); m.put("flgDefault",warehouseResponses.get(i).getFlgDefault()); m.put("cpId",cpId); m.put("opCreateUserId",staffId); listWarehouses.add(m); } map.put("warehouses",listWarehouses); // 基础资料(仓库 渠道 ) List saleChannelResponses = saleChannelMapper.selectByCond(new SaleChannelQuery()); List> listSaleChannels = new ArrayList<>(); for (int i = 0; i < saleChannelResponses.size(); i++) { m = new HashMap<>(); m.put("channelCode",saleChannelResponses.get(i).getChannelCode()); m.put("channelName",saleChannelResponses.get(i).getChannelName()); m.put("displayNo",saleChannelResponses.get(i).getDisplayNo()); m.put("flgDefault",saleChannelResponses.get(i).getFlgDefault()); m.put("cpId",cpId); m.put("opCreateUserId",staffId); listSaleChannels.add(m); } map.put("saleChannels",listSaleChannels); // 基础资料(仓库 渠道 收入 支出 入库 出库 账户 来源 盈亏原因) List dictionaryDataResponses = dictionaryDataMapper.selectByCond(new DictionaryDataQuery()); List> listDictionaryDatas = new ArrayList<>(); for (int i = 0; i < dictionaryDataResponses.size(); i++) { m = new HashMap<>(); m.put("dictCode",dictionaryDataResponses.get(i).getDictCode()); m.put("dataCode",dictionaryDataResponses.get(i).getDataCode()); m.put("dataValue",dictionaryDataResponses.get(i).getDataValue()); m.put("displayNo",dictionaryDataResponses.get(i).getDisplayNo()); m.put("flgDefault",dictionaryDataResponses.get(i).getFlgDefault()); m.put("cpId",cpId); m.put("opCreateUserId",staffId); listDictionaryDatas.add(m); } map.put("dictionaryDatas",listDictionaryDatas); // 计量单位 List unitResponses = unitMapper.selectByCond(new UnitQuery()); List> listUnits = new ArrayList<>(); for (int i = 0; i < unitResponses.size(); i++) { m = new HashMap<>(); m.put("unitCode",unitResponses.get(i).getUnitCode()); m.put("unitName",unitResponses.get(i).getUnitName()); m.put("decimalPlaces",unitResponses.get(i).getDecimalPlaces()); m.put("displayNo",unitResponses.get(i).getDisplayNo()); m.put("cpId",cpId); m.put("opCreateUserId",staffId); listUnits.add(m); } map.put("units",listUnits); // 种类 List goodsCategoryResponses = goodsCategoryMapper.selectByCond(new GoodsCategoryQuery()); List> listGoodsCategorys = new ArrayList<>(); for (int i = 0; i < goodsCategoryResponses.size(); i++) { m = new HashMap<>(); m.put("catCode",goodsCategoryResponses.get(i).getCatCode()); m.put("catName",goodsCategoryResponses.get(i).getCatName()); m.put("parentId",goodsCategoryResponses.get(i).getParentId()); m.put("topId",goodsCategoryResponses.get(i).getTopId()); m.put("levelUpper",goodsCategoryResponses.get(i).getLevelUpper()); m.put("levelLower",goodsCategoryResponses.get(i).getLevelLower()); m.put("levelLeaf",goodsCategoryResponses.get(i).getLevelLeaf()); m.put("levelNo",goodsCategoryResponses.get(i).getLevelNo()); m.put("levelCode",goodsCategoryResponses.get(i).getLevelCode()); m.put("levelName",goodsCategoryResponses.get(i).getLevelName()); m.put("displayNo",goodsCategoryResponses.get(i).getDisplayNo()); m.put("cpId",cpId); m.put("opCreateUserId",staffId); listGoodsCategorys.add(m); } map.put("goodsCategorys",listGoodsCategorys); // 打印票据 //List printLayoutResponses = printLayoutMapper.selectByCond(new PrintLayout()); //List> listPrintLayout = new ArrayList<>(); //for (int i = 0; i < printLayoutResponses.size(); i++) { // m = new HashMap<>(); // m.put("layoutName",printLayoutResponses.get(i).getLayoutName()); // m.put("layoutData",printLayoutResponses.get(i).getLayoutData()); // m.put("displayNo",printLayoutResponses.get(i).getDisplayNo()); // m.put("docName",printLayoutResponses.get(i).getDocName()); // m.put("cpId",cpId); // m.put("opCreateUserId",staffId); // listPrintLayout.add(m); //} //map.put("printLayout",listPrintLayout); ResponseResultVO listResponseResultVO = staffFeign.insertFeignCompanyData(map); // 如果没有成功返回 if (listResponseResultVO.getCode() != ResponseCodeEnum.SUCCESS.getCode()) { // 无用户 throw new BaseBusinessException(listResponseResultVO.getCode(), listResponseResultVO.getMessage()); } else { // 更新用户的角色ids--在插入基础数据的时候更新了。 // String rid = listResponseResultVO.getData(); // roleids.add(rid); } return ResponseResultUtil.success(company); } /** * @desc : 保存用户功能频率表 * @author : 周兴 * @date : 2024/3/28 9:39 */ @Transactional( rollbackFor = {Exception.class} ) public ResponseResultVO saveMenuFrequency(MenuFrequency menuFrequency) { MenuFrequency mf = companyMapper.selectMenuByUuid(menuFrequency); if(mf == null ){ menuFrequency.setQty(1); }else{ menuFrequency.setQty(mf.getQty() + 1); } // 保存用户功能频率表 companyMapper.saveMenuFrequency(menuFrequency); return ResponseResultUtil.success(); } }