| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481 |
- package com.dk.oauth.service.impl;
- import com.alibaba.fastjson.JSON;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.dk.common.exception.BaseBusinessException;
- import com.dk.common.infrastructure.constant.Constant;
- 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.common.util.AESUtil;
- import com.dk.oauth.convert.CompanyConvert;
- import com.dk.oauth.entity.*;
- 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.beans.factory.annotation.Autowired;
- 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.time.LocalDate;
- 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<CompanyMapper, Company> 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;
- @Resource
- MoneyAccountMapper moneyAccountMapper;
- @Resource
- PrintLayoutMapper printLayoutMapper;
- @Autowired
- private CompanyConvert companyConvert;
- @Autowired
- private TradeMapper tradeMapper;
- /**
- * @desc : 通过ID查询
- * @author : admin
- * @date : 2023/2/3 13:32
- */
- @Override
- public ResponseResultVO selectById(Integer id) {
- return ResponseResultUtil.success(companyMapper.selectById(id));
- }
- /**
- * @desc : 可以绑定微信的员工人数 人数上限
- * @author : admin
- * @date : 2023/2/3 13:32
- */
- @Override
- public ResponseResultVO getCompanyMaxStaffNum(Integer id) {
- Company company = companyMapper.selectById(id);
- Map<String,Object> mp = new HashMap<>();
- // 解析license信息
- String licenseStr = AESUtil.desEncrypt(company.getLicense());
- if(licenseStr != null ){
- Map<String,Object> licenseMap = JSON.parseObject(licenseStr,Map.class);
- mp.put("web_max_num",Integer.parseInt(licenseMap.get("web_max_num").toString()));
- mp.put("wx_max_num",Integer.parseInt(licenseMap.get("wx_max_num").toString()));
- }else{
- mp.put("web_max_num",1);
- mp.put("wx_max_num",1);
- }
- return ResponseResultUtil.success(mp);
- }
- /**
- * @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<String, Object> map) {
- String userId = map.get("userId") + "";
- Integer cpId = Integer.valueOf(map.get("cpId") + "");
- List<Integer> 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 updateCompanyCurStaffNum(Map<String, Object> map) {
- Integer curStaffNum = Integer.valueOf(map.get("curStaffNum") + "");
- Integer cpId = Integer.valueOf(map.get("cpId") + "");
- companyMapper.updateCompanyCurStaffNum(cpId, curStaffNum);
- return ResponseResultUtil.success(true);
- }
- /**
- * @desc : 选择公司更新微信用户的所在当前的公司
- * @author : admin
- * @date : 2023/2/3 13:32
- */
- @Override
- @Transactional(rollbackFor = Exception.class)
- public ResponseResultVO updateCurrentCpByWxid(Map<String, Object> 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(CompanyVO companyVO) {
- // 转实体
- Company company = companyConvert.convertToPo(companyVO);
- 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());
- }
- // company.setMaxStaffNum(1); // todo
- // company.setCurStaffNum(1);
- // 标准版默认2099年,1PC授权,1WX授权
- if(Constant.GradeCode.STD.getName().equals(company.getGradeCode())){
- company.setEndDate(LocalDate.parse("2099-12-31"));
- companyVO.setWebMaxNum(0);
- companyVO.setWxMaxNum(1);
- }
- company.setCpStatus("公司状态-通过");
- //获取具体服务的地址
- company.setSvcCode(companyMapper.selectServiceAllot(company.getGradeCode()));
- // 生成临时license授权
- Map<String,Object> licenseMap = new HashMap<>();
- licenseMap.put("grade_code",company.getGradeCode());
- licenseMap.put("end_date",company.getEndDate());
- licenseMap.put("web_max_num",companyVO.getWebMaxNum());
- licenseMap.put("wx_max_num",companyVO.getWxMaxNum());
- // 标准版
- if(Constant.GradeCode.STD.getName().equals(company.getGradeCode())){
- company.setLicense(AESUtil.aesEncrypt(JSON.toJSONString(licenseMap)) );
- }else{
- company.setLicenseSocial(AESUtil.aesEncrypt(JSON.toJSONString(licenseMap)) );
- }
- companyMapper.insert(company);
- // 动态设置服务的地址
- ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
- .getRequestAttributes();
- HttpServletRequest request;
- if (attributes != null) {
- request = attributes.getRequest();
- request.setAttribute("svcCode", company.getSvcCode().replace("_", "-"));
- }
- Trade trade = new Trade();
- // 专业版才插入交易记录
- if(Constant.GradeCode.PRO.getName().equals(company.getGradeCode())) {
- // 生成交易记录
- trade.setTradeNo(companyVO.getTradeNo());
- trade.setCpId(company.getCpId());
- trade.setWxUserId(companyVO.getWxUserId());
- trade.setTradeAmount(companyVO.getTradeAmount());
- trade.setTradeType(Constant.TradeType.REGISTER.getName());
- trade.setBuyLong(companyVO.getBuyLong());
- trade.setBuyBeginDate(companyVO.getBuyBeginDate());
- trade.setBuyEndDate(companyVO.getBuyEndDate());
- trade.setExtendDays(companyVO.getExtendDays());
- trade.setBuyGradeCode(companyVO.getGradeCode());
- trade.setTradeStatus(Constant.TradeStatus.INEFFECTIVE.getName());
- tradeMapper.insert(trade);
- }
- // 组织机构 -顶级 和公司的名称一样
- // 二级 名称叫做 运营中心 返回的业务部门的id是运营中新的id
- OrgVO orgVO = new OrgVO();
- orgVO.setCpId(company.getCpId());
- orgVO.setOrgName(company.getCpName());
- // 二级 名称叫做 运营中心 返回的业务部门的id是运营中新的id
- ResponseResultVO<OrgResponse> 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());
- // 240521 追加公司的版本 创建用户的时候用
- staffVO.setGradeCode(company.getGradeCode());
- // 生成默认的角色数组--// 更新用户的角色ids--在插入基础数据的时候更新了。
- // staffVO.setRoleIds();
- // 注册商户时候插入员工
- ResponseResultVO<StaffResponse> 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<Integer> 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<String, List<Map<String, Object>>> map = new HashMap<>();
- List<Map<String, Object>> listCom = new ArrayList<>();
- Map<String, Object> m = new HashMap<>();
- m.put("gradeCode",company.getGradeCode());
- m.put("cpId",cpId);
- m.put("staffId",staffId);
- listCom.add(m);
- map.put("company",listCom);
- // 角色及权限
- List<RoleResponse> roleResponses = roleMapper.selectByCond(new RoleQuery());
- List<Map<String, Object>> 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());
- listRoles.add(m);
- }
- map.put("roles",listRoles);
- // 基础资料(仓库 渠道 )
- List<WarehouseResponse> warehouseResponses = warehouseMapper.selectByCond(new WarehouseQuery());
- List<Map<String, Object>> 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());
- listWarehouses.add(m);
- }
- map.put("warehouses",listWarehouses);
- // 基础资料(仓库 渠道 )
- List<SaleChannelResponse> saleChannelResponses = saleChannelMapper.selectByCond(new SaleChannelQuery());
- List<Map<String, Object>> 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());
- listSaleChannels.add(m);
- }
- map.put("saleChannels",listSaleChannels);
- // 基础资料(仓库 渠道 收入 支出 入库 出库 账户 来源 盈亏原因)
- List<DictionaryDataResponse> dictionaryDataResponses = dictionaryDataMapper.selectByCond(new DictionaryDataQuery());
- List<Map<String, Object>> 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());
- listDictionaryDatas.add(m);
- }
- map.put("dictionaryDatas",listDictionaryDatas);
- // 资金账户
- List<MoneyAccountResponse> moneyAccountResponses = moneyAccountMapper.selectByCond(new MoneyAccountQuery());
- List<Map<String, Object>> listMoneyAccounts = new ArrayList<>();
- for (int i = 0; i < moneyAccountResponses.size(); i++) {
- m = new HashMap<>();
- m.put("macCode",moneyAccountResponses.get(i).getMacCode());
- m.put("macName",moneyAccountResponses.get(i).getMacName());
- m.put("macType",moneyAccountResponses.get(i).getMacType());
- m.put("balance",moneyAccountResponses.get(i).getBalance());
- m.put("accDate",moneyAccountResponses.get(i).getAccDate());
- m.put("displayNo",moneyAccountResponses.get(i).getDisplayNo());
- m.put("flgDefault",moneyAccountResponses.get(i).getFlgDefault());
- m.put("flgNegative",moneyAccountResponses.get(i).getFlgNegative());
- listMoneyAccounts.add(m);
- }
- map.put("moneyAccounts",listMoneyAccounts);
- // 计量单位
- List<UnitResponse> unitResponses = unitMapper.selectByCond(new UnitQuery());
- List<Map<String, Object>> 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());
- listUnits.add(m);
- }
- map.put("units",listUnits);
- // 种类
- List<GoodsCategoryResponse> goodsCategoryResponses = goodsCategoryMapper.selectByCond(new GoodsCategoryQuery());
- List<Map<String, Object>> listGoodsCategorys = new ArrayList<>();
- for (int i = 0; i < goodsCategoryResponses.size(); i++) {
- m = new HashMap<>();
- m.put("catId",goodsCategoryResponses.get(i).getCatId());
- 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());
- listGoodsCategorys.add(m);
- }
- map.put("goodsCategorys",listGoodsCategorys);
- // 打印票据
- List<PrintLayout> printLayoutResponses = printLayoutMapper.selectByCond(new PrintLayout());
- List<Map<String, Object>> 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);
- }
- companyVO.setCpId(company.getCpId()).setTradeAmount(trade.getTradeAmount()).setTradeNo(trade.getTradeNo());
- return ResponseResultUtil.success(companyVO);
- }
- /**
- * @desc : 续费
- * @author : 周兴
- * @date : 2024-05-17 13:55
- */
- @Transactional(rollbackFor = Exception.class)
- public ResponseResultVO reNewCompany(CompanyVO companyVO){
- // 转实体
- Company company = companyConvert.convertToPo(companyVO);
- // 生成临时license授权
- Map<String,Object> licenseMap = new HashMap<>();
- licenseMap.put("grade_code",company.getGradeCode());
- licenseMap.put("end_date",company.getEndDate());
- licenseMap.put("user_end_date",companyVO.getUserEndDate());
- licenseMap.put("web_max_num",companyVO.getWebMaxNum());
- licenseMap.put("wx_max_num",companyVO.getWxMaxNum());
- licenseMap.put("vip",companyVO.getVip());
- company.setLicenseSocial(AESUtil.aesEncrypt(JSON.toJSONString(licenseMap)) );
- companyMapper.updateById(company);
- // 生成交易记录
- Trade trade = new Trade();
- trade.setTradeNo(companyVO.getTradeNo());
- trade.setCpId(companyVO.getCpId());
- trade.setWxUserId(companyVO.getWxUserId());
- trade.setTradeAmount(companyVO.getTradeAmount());
- trade.setTradeType(Constant.TradeType.UPGRADE.getName());
- trade.setBuyLong(companyVO.getBuyLong());
- trade.setBuyBeginDate(companyVO.getBuyBeginDate());
- trade.setBuyEndDate(companyVO.getBuyEndDate());
- trade.setExtendDays(companyVO.getExtendDays());
- trade.setBuyGradeCode(companyVO.getGradeCode());
- trade.setTradeStatus(Constant.TradeStatus.INEFFECTIVE.getName());
- tradeMapper.insert(trade);
- return ResponseResultUtil.success(trade);
- }
- /**
- * @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();
- }
- }
|