package com.dk.mdm.service.mst; import com.alibaba.fastjson.JSONObject; import com.dk.common.exception.BaseBusinessException; import com.dk.common.infrastructure.annotaiton.Pagination; import com.dk.common.infrastructure.constant.Constant; import com.dk.common.infrastructure.enums.ErrorCodeEnum; import com.dk.common.model.pojo.PageList; import com.dk.common.model.pojo.link.CompanyLink; import com.dk.common.model.pojo.link.LinkApi; 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.common.util.HaipHttpUtils; import com.dk.mdm.mapper.mst.OrgMapper; import com.dk.mdm.mapper.mst.UserMapper; import com.dk.mdm.mapper.mst.WxUserMapper; import com.dk.mdm.model.pojo.mst.Company; import com.dk.mdm.mapper.mst.CompanyMapper; import com.dk.common.service.BaseService; import com.dk.common.mapper.BaseMapper; import com.dk.mdm.model.pojo.mst.Org; import com.dk.mdm.model.pojo.mst.WxUser; import com.dk.mdm.model.query.mst.CompanyQuery; import com.dk.mdm.model.response.mst.CompanyResponse; import org.apache.commons.collections4.CollectionUtils; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.client.RestTemplate; 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; @Service @Transactional public class CompanyService extends BaseService { @Override public BaseMapper getRepository() { return companyMapper; } @Autowired private CompanyMapper companyMapper; @Autowired private WxUserMapper wxuserMapper; @Resource private HaipHttpUtils haipHttpUtils; /** * @desc : 重写主键 * @author : 常皓宁 * @date : 2024/2/29 20:29 */ @Override public String getPrimaryKey() { return "cp_id"; } /** * @desc : 查询 * @author : 常皓宁 * @date : 2023/1/5 9:39 */ @Pagination public ResponseResultVO> selectByCond(CompanyQuery companyQuery) { return super.mergeListWithCount(companyQuery, companyMapper.selectByCond(companyQuery), companyMapper.countByCond(companyQuery)); } /** * @desc : 新建商户 * @author : 常皓宁 * @date : 2024/4/30 10:19 */ @Transactional(rollbackFor = Exception.class) public ResponseResultVO insertCompany(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("_", "-")); } //调用外部接口 return this.insertDkicOrg(company); } /** * @desc : 新建业务端组织机构和业务员 * @author : 常皓宁 * @date : 2024/4/30 13:02 */ @Transactional(rollbackFor = {Exception.class}) public ResponseResultVO insertDkicOrg(Company company) { // 设置请求地址 String requestUrl = "http://192.168.1.24:7001/" + Constant.MDM_PREFIX + Constant.SERVER + "-" + company.getSvcCode() + Constant.MST_ORG + "/insert_feign_org"; OrgVO orgVO = new OrgVO(); orgVO.setCpId(company.getCpId()); orgVO.setOrgName(company.getCpName()); OrgResponse orgResponse = new OrgResponse(); JSONObject OrgJsonObject = haipHttpUtils.doPostRequest(requestUrl, orgVO); if (OrgJsonObject.get("code") == null || !ResponseCodeEnum.SUCCESS.getCode().equals(OrgJsonObject.get("code"))) { // 无用户 throw new BaseBusinessException(ErrorCodeEnum.COMPANY_REGISTER_ERROR.getCode(), ErrorCodeEnum.COMPANY_REGISTER_ERROR.getMessage()); } else { Object data = OrgJsonObject.get("data"); orgResponse = JSONObject.parseObject(JSONObject.toJSONString(data),OrgResponse.class); } //新建微信用户 WxUser wxUser = wxuserMapper.getByPhone(company.getCpPhone()); WxUser newWxUser = new WxUser(); if (wxUser == null) { //注册 List joinedCps = new ArrayList<>(); joinedCps.add(company.getCpId()); newWxUser.setCurrentCp(company.getCpId()); newWxUser.setJoinedCps(joinedCps); newWxUser.setUserName(company.getCpManager()); newWxUser.setUserPhone(company.getCpPhone()); wxuserMapper.insert(newWxUser); } else { // 更新 户用户的公司id Integer cpId = company.getCpId(); List cpIds = new ArrayList<>(); cpIds.add(cpId); if (wxUser.getJoinedCps()!=null && wxUser.getJoinedCps().size() > 0){ cpIds.addAll(wxUser.getJoinedCps()) ; } wxuserMapper.updateCpid(wxUser.getUserId(), cpId, cpIds); } // 生成员工 StaffVO staffVO = new StaffVO(); //TODO: 获取新建或者更新的ID 得想想怎么弄 staffVO.setWxUserId(wxUser.getUserId()); staffVO.setCpId(company.getCpId()); staffVO.setStaffName(company.getCpManager()); staffVO.setStaffPhone(company.getCpPhone()); staffVO.setOrgId(orgResponse.getOrgId()); // 注册商户时候插入员工 StaffResponse staffResponse = new StaffResponse(); JSONObject staffJsonObject = haipHttpUtils.doPostRequest(requestUrl, staffVO); // 如果没有成功返回,状态设置为待审 if (staffJsonObject.get("code") == null || !ResponseCodeEnum.SUCCESS.getCode().equals(staffJsonObject.get("code"))) { // 无用户 throw new BaseBusinessException(ErrorCodeEnum.USER_REGISTER_ERROR.getCode(), ErrorCodeEnum.USER_REGISTER_ERROR.getMessage()); } else { Object data = staffJsonObject.get("data"); staffResponse = JSONObject.parseObject(JSONObject.toJSONString(data),StaffResponse.class); } // 生成员工权限 角色 仓库 等等。 return ResponseResultUtil.success(company); } }