package com.hssx.cloudmodel.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hssx.cloudmodel.constant.Constant;
import com.hssx.cloudmodel.entity.*;
import com.hssx.cloudmodel.entity.vo.CompanyVO;
import com.hssx.cloudmodel.entity.vo.UserVO;
import com.hssx.cloudmodel.mapper.*;
import com.hssx.cloudmodel.service.CompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hssx.cloudmodel.util.HttpRespMsg;
import com.hssx.cloudmodel.util.ListUtil;
import com.hssx.cloudmodel.util.PageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.plugin.com.event.COMEventHandler;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
*
* 服务实现类
*
*
* @author 吴涛涛
* @since 2019-07-26
*/
@Service
public class CompanyServiceImpl extends ServiceImpl implements CompanyService {
@Resource
CompanyMapper companyMapper;
@Resource
UserMapper userMapper;
@Resource
LngLatCompanyMapper lngLatCompanyMapper;
@Resource
ProjectMapper projectMapper;
@Resource
MouldMapper mouldMapper;
@Resource
ProjectUserMapper projectUserMapper;
@Resource
ProjectApproveMapper projectApproveMapper;
@Resource
AssetCustomCompanyMapper assetCustomCompanyMapper;
@Resource
CustomCompanyMapper customCompanyMapper;
@Override
public HttpRespMsg addAndUpdateRole(CompanyVO companyVO, Integer flag, String companyIds) {
HttpRespMsg msg = new HttpRespMsg();
Company company = new Company();
if (flag == 0) {
//添加公司
QueryWrapper qw = new QueryWrapper<>();
qw.eq("company_name", companyVO.getCompanyName());
int count = companyMapper.selectCount(qw);
if (count > 0) {
msg.setError("公司已存在,请勿重复添加");
} else {
if (companyVO.getCompanyType() == 0) {
//资产方直接添加
BeanUtils.copyProperties(companyVO, company);
companyMapper.insert(company);
if (companyIds != null && !"".equals(companyIds)) {
List ides = ListUtil.convertIntegerIdsArrayToList(companyIds);
for (Integer ide : ides) {
if (assetCustomCompanyMapper.selectCount(new QueryWrapper().eq("assert_id", company.getId()).eq("custom_id", ide)) == 0) {
AssetCustomCompany assetCustomCompany = new AssetCustomCompany();
assetCustomCompany.setAssertId(company.getId());
assetCustomCompany.setCustomId(ide);
assetCustomCompanyMapper.insert(assetCustomCompany);
}
}
}
} else if (companyVO.getCompanyType() == 1) {
//生产方
BeanUtils.copyProperties(companyVO, company);
companyMapper.insert(company);
if (companyIds != null && !"".equals(companyIds)) {
List ides = ListUtil.convertIntegerIdsArrayToList(companyIds);
for (Integer ide : ides) {
if (assetCustomCompanyMapper.selectCount(new QueryWrapper().eq("assert_id", ide).eq("custom_id", company.getId())) == 0) {
AssetCustomCompany assetCustomCompany = new AssetCustomCompany();
assetCustomCompany.setAssertId(ide);
assetCustomCompany.setCustomId(company.getId());
assetCustomCompanyMapper.insert(assetCustomCompany);
}
}
}
LngLatCompany factory = new LngLatCompany();
factory.setxLat(companyVO.getXLat());
factory.setyLng(companyVO.getYLng());
factory.setCompanyId(company.getId());
lngLatCompanyMapper.insert(factory);
}
msg.data = company;
}
} else if (flag == 1) {
//更新公司信息
BeanUtils.copyProperties(companyVO, company);
if (companyVO.getCompanyType() == 0) {
//修改资产方
BeanUtils.copyProperties(companyVO, company);
List list = assetCustomCompanyMapper.selectList(new QueryWrapper().eq("assert_id", company.getId()));
//判断所关联的生产方是否已被应用到项目
List idess = new ArrayList<>();
idess.add(-1);
for (AssetCustomCompany assetCustomCompany : list) {
idess.add(assetCustomCompany.getCustomId());
}
Integer count = customCompanyMapper.selectCount(new QueryWrapper().in("company_id", idess));
if (count > 0) {
msg.setError("当前公司所生产方公司已被用用到项目,不可执行修改操作。");
return msg;
}
//删除之前的关联公司
assetCustomCompanyMapper.delete(new QueryWrapper().eq("assert_id",companyVO.getId()));
if (companyIds != null && !"".equals(companyIds)) {
List ides = ListUtil.convertIntegerIdsArrayToList(companyIds);
for (Integer ide : ides) {
if (assetCustomCompanyMapper.selectCount(new QueryWrapper().eq("assert_id", company.getId()).eq("custom_id", ide)) == 0) {
AssetCustomCompany assetCustomCompany = new AssetCustomCompany();
assetCustomCompany.setAssertId(company.getId());
assetCustomCompany.setCustomId(ide);
assetCustomCompanyMapper.insert(assetCustomCompany);
}
}
}
companyMapper.updateById(company);
} else if (companyVO.getCompanyType() == 1) {
//生产方
BeanUtils.copyProperties(companyVO, company);
companyMapper.updateById(company);
QueryWrapper qw = new QueryWrapper<>();
qw.eq("company_id", companyVO.getId());
LngLatCompany lngLatCompany = lngLatCompanyMapper.selectOne(qw);
LngLatCompany factory = new LngLatCompany();
factory.setxLat(companyVO.getXLat());
factory.setyLng(companyVO.getYLng());
factory.setCompanyId(company.getId());
factory.setId(lngLatCompany.getId());
lngLatCompanyMapper.updateById(factory);
List list = assetCustomCompanyMapper.selectList(new QueryWrapper().eq("assert_id", company.getId()));
//判断所关联的生产方是否已被应用到项目
List idess = new ArrayList<>();
idess.add(-1);
for (AssetCustomCompany assetCustomCompany : list) {
idess.add(assetCustomCompany.getCustomId());
}
Integer count = customCompanyMapper.selectCount(new QueryWrapper().eq("company_id", company.getId()));
if (count > 0) {
msg.setError("当前公司已和所关联的资产方合作相关项目,不可执行修改操作。");
return msg;
}
//删除之前的关联公司
assetCustomCompanyMapper.delete(new QueryWrapper().eq("custom_id",companyVO.getId()));
if (companyIds != null && !"".equals(companyIds)) {
List ides = ListUtil.convertIntegerIdsArrayToList(companyIds);
for (Integer ide : ides) {
if (assetCustomCompanyMapper.selectCount(new QueryWrapper().eq("assert_id", company.getId()).eq("custom_id", ide)) == 0) {
AssetCustomCompany assetCustomCompany = new AssetCustomCompany();
assetCustomCompany.setAssertId(company.getId());
assetCustomCompany.setCustomId(ide);
assetCustomCompanyMapper.insert(assetCustomCompany);
}
}
}
}
msg.data = company;
}
return msg;
}
@Override
public HttpRespMsg pageList(PageUtil page, String keyName, Integer companyType) {
HttpRespMsg msg = new HttpRespMsg();
List list = new ArrayList<>();
PageHelper.startPage(page.getPageNum(),page.getPageSize());
list = companyMapper.getCustomerListByKeyName(keyName, companyType);
PageInfo pageInfos = new PageInfo<>(list);
msg.data = pageInfos;
return msg;
}
@Override
public List getIdAndNamelist(User user) {
QueryWrapper qw = new QueryWrapper<>();
qw.select("id", "company_name", "company_type");
if (Constant.SYS_ID == user.getParentId()) {
//此时是admin创建用户,返回可选的生产方公司,查询出当前admin的信息并获取他的公司id
User admin = userMapper.selectOne(new QueryWrapper().eq("id", user.getId()));
qw.eq("company_type", Constant.PRODUCER_COMPANY).or().eq("id", admin.getCompanyId());
} else if (Constant.SYS_PARENT_ID == user.getParentId()) {
//系统管理员创建admin,返回资产方公司
qw.eq("company_type", Constant.ASSETS_COMPANY);
} else {
//此时是项目经理创建其他用户
User admin = userMapper.selectOne(new QueryWrapper().eq("id", user.getParentId()));
qw.eq("company_type", Constant.PRODUCER_COMPANY).or().eq("id", admin.getCompanyId());
}
return companyMapper.selectList(qw);
}
@Override
public List addCompanyListToProject(User user) {
QueryWrapper qw = new QueryWrapper<>();
qw.eq("company_type", Constant.PRODUCER_COMPANY);
return companyMapper.selectList(qw);
}
@Override
public HttpRespMsg getCoutomCompanyAndMouldsByUser(UserVO userVO) {
HttpRespMsg msg = new HttpRespMsg();
List companyVOS = new ArrayList<>();
List mouldIds = new ArrayList<>();
mouldIds.add(-1);
User currentUser = userMapper.selectOne(new QueryWrapper().eq("head_imgurl", userVO.getToken()));
if (Constant.SYS_ID == currentUser.getParentId()) {
//此时是admin,可看到自己公司下的模具和交于生产的公司
QueryWrapper qw = new QueryWrapper<>();
List moulds = mouldMapper.selectList(qw.eq("company_id", currentUser.getCompanyId()));
for (Mould mould : moulds) {
mouldIds.add(mould.getId());
}
companyVOS = companyMapper.getListMould(mouldIds);
} else if (Constant.SYS_PARENT_ID == currentUser.getParentId()) {
//系统管理员创建admin,返回资产方公司
List moulds = mouldMapper.selectList(new QueryWrapper());
for (Mould mould : moulds) {
mouldIds.add(mould.getId());
}
companyVOS = companyMapper.getListMould(mouldIds);
} else {
//此时是项目经理
QueryWrapper qw = new QueryWrapper<>();
qw.eq("manager_id", currentUser.getId());
List set = new ArrayList<>();
set.add(-1);
List projects = projectMapper.selectList(qw);
if (projects.size() > 0) {
for (Project project : projects) {
set.add(project.getId());
}
}
// //充当普通人员参与的项目
List projectUsers = projectUserMapper.selectList(new QueryWrapper().eq("user_id", currentUser.getId()));
if (projectUsers.size() > 0) {
for (ProjectUser projectUser : projectUsers) {
set.add(projectUser.getProjectId());
}
}
//充当审批人员参与的项目
List projectss = projectApproveMapper.selectList(new QueryWrapper().eq("approver_id", currentUser.getId()));
if (projectss.size() > 0) {
for (ProjectApprove projectUser : projectss) {
set.add(projectUser.getProjectId());
}
}
List moulds = mouldMapper.selectList(new QueryWrapper().in("project_id", set));
for (Mould mould : moulds) {
mouldIds.add(mould.getId());
}
companyVOS = companyMapper.getListMould(mouldIds);
}
msg.data = companyVOS;
return msg;
}
@Override
public HttpRespMsg relationList(Integer companyType) {
HttpRespMsg msg = new HttpRespMsg();
msg.data = companyMapper.selectList(new QueryWrapper().eq("company_type", companyType));
return msg;
}
@Override
public HttpRespMsg detail(UserVO userVO) {
HttpRespMsg msg = new HttpRespMsg();
// Company company = companyMapper.selectById(userVO.getCompanyId());
// if (company != null) {
// if (0 == company.getCompanyType()) {
// //资产方
// CompanyVO
//
// }
// } else {
// msg.setError("公司不存在");
// }
return msg;
}
@Override
public HttpRespMsg getOwnerRelateCompany(CompanyVO companyVO) {
HttpRespMsg msg = new HttpRespMsg();
User currentUser = userMapper.selectOne(new QueryWrapper().eq("head_imgurl", companyVO.getToken()));
List ides = new ArrayList<>();
ides.add(-1);
if(null == currentUser){
msg.setError("当前用户不存在或者不存在");
return msg;
}else{
Company company = companyMapper.selectById(companyVO.getId());
if(Constant.PRODUCER_COMPANY == company.getCompanyType()){
//生产方公司,返回关联的资产方公司列表
ides = assetCustomCompanyMapper.selectList(new QueryWrapper()
.eq("custom_id", company.getId()))
.stream()
.map(AssetCustomCompany::getAssertId)
.collect(Collectors.toList());
msg.data = companyMapper.selectList(new QueryWrapper().in("id",ides));
}else if(Constant.ASSETS_COMPANY == company.getCompanyType()){
//资产方公司,返回关联的生产方公司列表
ides = assetCustomCompanyMapper.selectList(new QueryWrapper()
.eq("assert_id", company.getId()))
.stream()
.map(AssetCustomCompany::getCustomId)
.collect(Collectors.toList());
msg.data = companyMapper.selectList(new QueryWrapper().in("id",ides));
}
}
return msg;
}
@Override
public HttpRespMsg getProduceCompany(UserVO userVO) {
HttpRespMsg msg = new HttpRespMsg();
User currentUser = userMapper.selectOne(new QueryWrapper().eq("head_imgurl", userVO.getToken()));
if(null == currentUser){
msg.setError("当前用户不存在或者不存在");
return msg;
}else{
msg.data = customCompanyMapper.selectList(new QueryWrapper().eq("project_id",userVO.getProjectId()));
}
return msg;
}
@Override
public HttpRespMsg deleteById(Integer id) {
HttpRespMsg msg = new HttpRespMsg();
Integer count = userMapper.selectCount(new QueryWrapper().eq("company_id", id));
if (count > 0) {
msg.setError("该公司已被应用到用户中,暂不提供删除操作");
return msg;
} else {
Company company = companyMapper.selectById(id);
companyMapper.deleteById(id);
if(company.getCompanyType()==0){
//删除资产方关联的生产方公司
assetCustomCompanyMapper.delete(new QueryWrapper().eq("assert_id",id));
}else if(company.getCompanyType()==1){
assetCustomCompanyMapper.delete(new QueryWrapper().eq("custom_id",id));
}
}
return msg;
}
}