package com.chuanghai.attendance.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.chuanghai.attendance.common.exception.BizCodeEnume; import com.chuanghai.attendance.common.exception.RRException; import com.chuanghai.attendance.common.utils.CommonResult; import com.chuanghai.attendance.dao.WorkerIdentityDao; import com.chuanghai.attendance.entity.WorkerIdentity; import com.chuanghai.attendance.service.WorkerIdentityService; import com.chuanghai.attendance.utils.excel.ExcelImportXLSXUtil; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartFile; import java.util.ArrayList; import java.util.List; /** * @Author: binguo * @Date: 2022/9/20 星期二 17:16 * @Description: com.chuanghai.attendance.service.impl * @Version: 1.0 */ @Service("workerIdentityService") public class WorkerIdentityServiceImpl extends ServiceImpl implements WorkerIdentityService { @Override public CommonResult importWorkExcel(MultipartFile file) throws Exception { //导入数据前先删除原先的数据 deleteIdCard(); List workerIdList = new ArrayList<>(); List list = ExcelImportXLSXUtil.readerExcel(file.getInputStream(), "员工身份", 4); if(list == null){ return CommonResult.fail("-1","缺少员工身份页数据!"); } Integer count = 0; for (String[] strings : list) { count++; if (count < 2) { continue; } WorkerIdentity workerIdentity = new WorkerIdentity(); workerIdentity.setWorkNum(strings[0]); workerIdentity.setWorkName(strings[1]); workerIdentity.setIdCard(strings[2]); workerIdentity.setUserId(strings[3]); // WorkerIdentity queryByIdCard = queryByIdCard(strings[2],strings[1]); // if (queryByIdCard == null) { // } workerIdList.add(workerIdentity); } if (workerIdList.size() > 0 ) { boolean d = this.saveBatch(workerIdList, workerIdList.size()); return d ? CommonResult.ok() : CommonResult.fail("-1","缺少员工身份页数据!"); }else { return CommonResult.ok(); } } public void deleteIdCard() { List list = this.list(); List idList = new ArrayList<>(); list.forEach(workerIdentity -> { idList.add(workerIdentity.getId()) ; }); this.removeByIds(idList); } @Override public String queryByuserId(String userId, String workNum, String workName) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("user_id",userId); // System.out.println(userId); WorkerIdentity one = this.getOne(wrapper); if (one == null) { if (StringUtils.hasText(workNum)) { QueryWrapper wrapper1 = new QueryWrapper<>(); wrapper1.eq("work_num",workNum); one = this.getOne(wrapper1); } } if (one == null) { throw new RRException(BizCodeEnume.DATA_IS_NOT_EXIST, "用户 "+workName+" userId不存在"); } String idCard = one.getIdCard(); return idCard; } @Override public List queryByuserIds(List userIds, List workNums) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.in("user_id", userIds).or().in("work_num", workNums); List list = this.list(wrapper); if (list.size() == 0) { // throw new RRException(BizCodeEnume.DATA_IS_NOT_EXIST, userId + "userId workNum不存在"); } return list; } public WorkerIdentity queryByIdCard(String idCard,String workName) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("id_card", idCard); WorkerIdentity workerIdentity; try { workerIdentity = this.getOne(wrapper); }catch (Exception e){ throw new RRException(BizCodeEnume.DATA_IS_EXIST, "员工 " +workName+ " 身份证信息重复"); } return workerIdentity; } }