jiaosong
2023-08-18 26e4b49b3b51e123e9ce2b055c8e0d37d35df260
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package doumeemes.service.business.impl;
 
import doumeemes.core.constants.ResponseStatus;
import doumeemes.core.exception.BusinessException;
import doumeemes.core.model.LoginUserInfo;
import doumeemes.core.model.PageData;
import doumeemes.core.model.PageWrap;
import doumeemes.core.utils.Constants;
import doumeemes.core.utils.Utils;
import doumeemes.core.utils.excel.EasyExcelUtil;
import doumeemes.dao.business.*;
import doumeemes.dao.business.model.*;
import doumeemes.dao.business.vo.SalaryParamVO;
import doumeemes.dao.ext.MaterialExtMapper;
import doumeemes.dao.ext.dto.SalaryParamDTO;
import doumeemes.dao.ext.dto.SalaryParamImportDTO;
import doumeemes.dao.ext.vo.DepartmentExtListVO;
import doumeemes.service.business.DepartmentService;
import doumeemes.service.business.SalaryParamService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import doumeemes.service.ext.DepartmentExtService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
 
import java.math.BigDecimal;
import java.util.*;
 
/**
 * 设置类-绩效工资配置表Service实现
 * @author 江蹄蹄
 * @date 2023/08/11 15:29
 */
@Service
public class SalaryParamServiceImpl implements SalaryParamService {
 
    @Autowired
    private SalaryParamMapper salaryParamMapper;
 
    @Autowired
    private DepartmentMapper departmentMapper;
    @Lazy
    @Autowired
    private DepartmentExtService departmentExtService;
 
    @Autowired
    private MaterialMapper materialMapper;
    @Autowired
    private MaterialDistributeMapper materialDistributeMapper;
 
    @Autowired
    private ProceduresMapper proceduresMapper;
 
    @Override
    public Integer create(SalaryParam salaryParam) {
 
 
        LoginUserInfo principal = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
 
        SalaryParam insert = new SalaryParam();
        insert.setRootDepartId(principal.getRootDepartment().getId());
        insert.setDepartId(salaryParam.getDepartId());
        insert.setProcedureId(salaryParam.getProcedureId());
        insert.setMaterialId(salaryParam.getMaterialId());
        insert.setDeleted((byte) Constants.ZERO);
        insert.setType(salaryParam.getType());
        if(findOne(insert) != null){
            throw  new BusinessException(ResponseStatus.DATA_EXISTS.getCode(),"同一个物料在相同工序不能重复配置哦!");
        }
        insert.setCreateUser(principal.getId());
        insert.setCreateTime(new Date());
        insert.setUpdateUser(principal.getId());
        insert.setUpdateTime(new Date());
        insert.setRemark(salaryParam.getRemark());
        insert.setSalary(salaryParam.getSalary().multiply(new BigDecimal("100")));
        insert.setNum(salaryParam.getNum());
        insert.setTimes(salaryParam.getTimes());
        insert.setUnqualified(salaryParam.getUnqualified());
        insert.setType(salaryParam.getType());
        salaryParamMapper.insert(insert);
        return salaryParam.getId();
    }
 
    @Override
    public void deleteById(Integer id) {
        salaryParamMapper.deleteById(id);
    }
 
    @Override
    public void delete(SalaryParam salaryParam) {
        UpdateWrapper<SalaryParam> deleteWrapper = new UpdateWrapper<>(salaryParam);
        salaryParamMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        salaryParamMapper.deleteBatchIds(ids);
    }
 
    @Override
    public void updateById(SalaryParam salaryParam) {
        salaryParamMapper.updateById(salaryParam);
    }
 
    @Override
    public void update(SalaryParam salaryParam) {
        LoginUserInfo principal = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        UpdateWrapper<SalaryParam> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(SalaryParam::getId,salaryParam.getId())
                .set(SalaryParam::getUpdateTime,new Date())
                .set(SalaryParam::getUpdateUser,principal.getId())
                .set(SalaryParam::getType,salaryParam.getType())
//                .set(SalaryParam::getDepartId,salaryParam.getDepartId())
//                .set(SalaryParam::getMaterialId,salaryParam.getMaterialId())
//                .set(SalaryParam::getProcedureId,salaryParam.getProcedureId())
                .set(SalaryParam::getSalary,salaryParam.getSalary().multiply(new BigDecimal(100)))
                .set(SalaryParam::getNum,salaryParam.getNum())
                .set(SalaryParam::getTimes,salaryParam.getTimes())
                .set(SalaryParam::getUnqualified,salaryParam.getUnqualified());
        salaryParamMapper.update(null,wrapper);
    }
 
    @Override
    public void updateByIdInBatch(List<SalaryParam> salaryParams) {
        if (CollectionUtils.isEmpty(salaryParams)) {
            return;
        }
        for (SalaryParam salaryParam: salaryParams) {
            this.updateById(salaryParam);
        }
    }
 
    @Override
    public SalaryParam findById(Integer id) {
        return salaryParamMapper.selectById(id);
    }
 
    @Override
    public SalaryParam findOne(SalaryParam salaryParam) {
        QueryWrapper<SalaryParam> wrapper = new QueryWrapper<>(salaryParam);
        wrapper.last("limit 1");
        return salaryParamMapper.selectOne(wrapper);
    }
 
    @Override
    public List<SalaryParam> findList(SalaryParam salaryParam) {
        QueryWrapper<SalaryParam> wrapper = new QueryWrapper<>(salaryParam);
        return salaryParamMapper.selectList(wrapper);
    }
 
    @Override
    public PageData<SalaryParamVO> findPage(PageWrap<SalaryParamDTO> pageWrap) {
        IPage<SalaryParam> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        LoginUserInfo principal = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        SalaryParamDTO model = pageWrap.getModel();
        model.setRootDepartId(principal.getRootDepartment().getId());
//        model.setDepartId(principal.getComDepartment().getId());
        IPage<SalaryParamVO> salaryParamVOIPage = salaryParamMapper.selectPage(page, model);
        if (!CollectionUtils.isEmpty(salaryParamVOIPage.getRecords())){
            salaryParamVOIPage.getRecords().forEach(s->s.setSalary(s.getSalary().divide(new BigDecimal(100))));
        }
        return PageData.from(salaryParamVOIPage);
    }
 
    @Override
    public long count(SalaryParam salaryParam) {
        QueryWrapper<SalaryParam> wrapper = new QueryWrapper<>(salaryParam);
        return salaryParamMapper.selectCount(wrapper);
    }
 
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    @Override
    public void importPlans(MultipartFile file) {
 
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(!Constants.equalsInteger(user.getType(),Constants.USERTYPE.COM)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!");
        }
        //解析excel
        List<SalaryParamImportDTO> plansList = EasyExcelUtil.importExcel(file, 1, 1, SalaryParamImportDTO.class);
        if(plansList == null || plansList.size()==0){
            throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "导入数据内容有误!");
        }
 
        plansList.forEach(s->{
            if (Objects.isNull(s.getType())
                || Objects.isNull(s.getDepartName())
                || Objects.isNull(s.getMaterialCode())
                || Objects.isNull(s.getProcedureName())
                || Objects.isNull(s.getNum())){
                throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "导入数据内容有误!");
            }
        });
 
 
        for (int i = 0; i < plansList.size(); i++) {
            SalaryParamImportDTO salaryParamImportDTO = plansList.get(i);
//
//            QueryWrapper<Department> wrapper = new QueryWrapper<>();
//            wrapper.lambda()
//                    .eq(Department::getName,salaryParamImportDTO.getDepartName())
//                    .eq(Department::getType,Constants.DEPART_TYPE.factory)
//                    .eq(Department::getDeleted,Constants.ZERO)
//                    .last("limit 1");
            Department dparam = new Department();
            dparam.setName(salaryParamImportDTO.getDepartName());
            dparam.setDeleted(Constants.ZERO);
            dparam.setType(Constants.DEPART_TYPE.factory);
            dparam = departmentExtService.findOne(dparam);
            if (Objects.isNull(dparam)){
                throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "导入数据内容【公司名称】有误!");
            }
            DepartmentExtListVO department = new DepartmentExtListVO();
            BeanUtils.copyProperties(dparam, department);
            Integer comDepartId = departmentExtService.getComDepartId(department);
 
            QueryWrapper<Material> materialWrapper = new QueryWrapper<>();
            materialWrapper.lambda()
                    .eq(Material::getCode,salaryParamImportDTO.getMaterialCode())
                    .eq(Material::getRootDepartId,department.getRootId())
                    .eq(Material::getDeleted,Constants.ZERO)
                    .last("limit 1");
            Material material = materialMapper.selectOne(materialWrapper);
 
            if (Objects.isNull(material)){
                throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "导入数据内容【产品编码】有误!");
            }
            if (Objects.nonNull(material) && !Objects.equals(material.getName(),salaryParamImportDTO.getMaterialName())){
                throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "导入数据内容【产品名称】有误!");
            }
            QueryWrapper<MaterialDistribute> materialDistributeQueryWrapper = new QueryWrapper<>();
            materialDistributeQueryWrapper.lambda()
                    .eq(MaterialDistribute::getMaterialId,material.getId())
                    .eq(MaterialDistribute::getRootDepartId,department.getRootId())
                    .eq(MaterialDistribute::getDepartId, comDepartId)
                    .eq(MaterialDistribute::getDeleted,Constants.ZERO)
                    .last("limit 1");
            MaterialDistribute materialDistribute = materialDistributeMapper.selectOne(materialDistributeQueryWrapper);
            if (Objects.isNull(materialDistribute)){
                throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "导入数据内容【产品编码】有误!");
            }
            QueryWrapper<Procedures> proceduresWrapper = new QueryWrapper<>();
            proceduresWrapper.lambda()
                    .eq(Procedures::getName,salaryParamImportDTO.getProcedureName())
                    .eq(Procedures::getRootDepartId,department.getRootId())
//                    .eq(Procedures::getOrgId,department.getId())
                    .last("limit 1");
            Procedures procedures = proceduresMapper.selectOne(proceduresWrapper);
            if (Objects.isNull(procedures)){
                throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "导入数据内容【工序名称】有误!");
            }
            SalaryParam salaryParam = new SalaryParam();
            salaryParam.setRootDepartId(department.getRootId());
            salaryParam.setDepartId(department.getId());
            salaryParam.setProcedureId(procedures.getId());
            salaryParam.setMaterialId(materialDistribute.getId());
            salaryParam.setSalary(salaryParamImportDTO.getSalary());
            salaryParam.setNum(new BigDecimal(salaryParamImportDTO.getNum()));
            salaryParam.setTimes(getIntegerTimes(salaryParamImportDTO.getTimesName()));
            Integer unqualified = Optional.ofNullable(salaryParamImportDTO.getUnqualified()).map(s -> {
                if ("否".equals(s)) {
                    return 0;
                } else {
                    return 1;
                }
            }).orElse(0);
            salaryParam.setUnqualified(unqualified);
 
            Integer type = Optional.ofNullable(salaryParamImportDTO.getType()).map(s -> {
                if ("计件".equals(s)) {
                    return 0;
                } else {
                    return 1;
                }
            }).orElse(0);
            salaryParam.setType(type);
            create(salaryParam);
        }
 
    }
 
    private Integer getIntegerTimes(String a) {
        int h =0,m=0,s=0;
        try{
            int hIndex =a.lastIndexOf("小时");
            int mIndex=a.lastIndexOf("分钟");
            int sIndex =a.lastIndexOf("秒");
            if( hIndex>0){
                h = Integer.parseInt(a.substring(0,hIndex));
            }
            if(mIndex > hIndex){
                m = Integer.parseInt(a.substring(hIndex >=0?hIndex+2:0,mIndex));
            }
            if(sIndex > mIndex){
               s = Integer.parseInt(a.substring(mIndex >= 0 ? mIndex+2 : 0, sIndex));
            }
            return  h*3600+m*60+s;
        }catch (Exception e){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,时长【"+a+"】格式不正确!" );
        }
 
    }
}