郑永安
2023-06-19 f65443d8abeaedc9d102324565e8368e7c9d90c8
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
package com.gk.firework.Service.ServiceImpl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.firework.Domain.ControlPrintParam;
import com.gk.firework.Domain.Exception.BusinessException;
import com.gk.firework.Domain.UserInfo;
import com.gk.firework.Domain.Utils.PageInfo;
import com.gk.firework.Domain.Utils.StringUtils;
import com.gk.firework.Mapper.ControlPrintParamMapper;
import com.gk.firework.Service.ControlPrintParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
@Service
public class ControlPrintParamServiceImpl extends ServiceImpl<ControlPrintParamMapper, ControlPrintParam> implements ControlPrintParamService {
    @Autowired
    ControlPrintParamMapper controlPrintParamMapper;
 
    private static final String defaultSettingsOwner = "admin";
    //内包装设置
    private static final byte inner = 1;
    //外包装设置
    private static final byte outer = 2;
 
    @Override
    public ControlPrintParam getParamsByUser(UserInfo userInfo, byte type) {
        LambdaQueryWrapper<ControlPrintParam> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ControlPrintParam::getOwner,userInfo.getUsername());
        wrapper.eq(ControlPrintParam::getType,type);
        ControlPrintParam controlPrintParam = baseMapper.selectOne(wrapper);
        if (controlPrintParam == null){
            wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ControlPrintParam::getOwner,"admin");
            wrapper.eq(ControlPrintParam::getType,type);
        }
        return baseMapper.selectOne(wrapper);
    }
 
    @Override
    public void selectDataGrid(PageInfo pageInfo) {
        Page<ControlPrintParam> page = new Page<>(pageInfo.getPageIndex(), pageInfo.getPageSize());
        List<OrderItem> orderItems = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        if (StringUtils.isNotBlank(pageInfo.getSort()) && StringUtils.isNotBlank(pageInfo.getOrder())) {
            orderItem.setAsc(pageInfo.getOrder().equalsIgnoreCase("ascending"));
            orderItem.setColumn(pageInfo.getSort());
        }else {
            orderItem.setAsc(false);
            orderItem.setColumn("id");
        }
        orderItems.add(orderItem);
        page.setOrders(orderItems);
        List<ControlPrintParam> res = controlPrintParamMapper.selectDataGrid(page,pageInfo.getCondition());
        //没有配置就返回默认配置
        List<ControlPrintParam> settings = new ArrayList<>(res);
        boolean flag = pageInfo.getCondition().get("enterprisename") == null &&
                pageInfo.getCondition().get("type") == null;
        //默认内包装
        if (flag && settings.stream().filter(item -> item.getType() == inner).count() < 1) {
            settings.addAll(this.selectDefaultSettings(inner,defaultSettingsOwner));
            page.setTotal(page.getTotal() + 1);
        }
        //默认外包装
        if (flag && settings.stream().filter(item -> item.getType() == outer).count() < 1) {
            settings.addAll(this.selectDefaultSettings(outer,defaultSettingsOwner));
            page.setTotal(page.getTotal() + 1);
        }
        pageInfo.setResult(settings);
        pageInfo.setTotalCount(page.getTotal());
    }
 
    /**
     * @Description: 获取内外包装默认设置,owner为admin
     * @param type 类型 null 查询所有
     * @date 2022/2/9 16:32
     */
    @Override
    public List<ControlPrintParam> selectDefaultSettings(Byte type,String owner) {
        return controlPrintParamMapper.selectList(
                new LambdaQueryWrapper<ControlPrintParam>()
                        .eq(ControlPrintParam::getOwner, owner)
                        .eq(ControlPrintParam::getType,type));
    }
 
    /**
    * @Description: 查找自己的type方案
    * @date 2022/2/11 14:02
    */
    @Override
    public ControlPrintParam getOwnSelectType(Byte type, UserInfo user) {
        if (type == null) throw new BusinessException("参数不能为空");
        List<ControlPrintParam> ownType = this.selectDefaultSettings(type, user.getUsername());
        if (ownType.size() > 1)  throw new BusinessException("配置重复,请检查数据");
        //没有配置返回默认配置
        ControlPrintParam param = new ControlPrintParam();
        param.setType(type);
        if (ownType.size() == 0) {
            List<ControlPrintParam> defaultSettings = this.selectDefaultSettings(type, defaultSettingsOwner);
            if (defaultSettings.size() > 0) param = defaultSettings.get(0);
        }
        if (ownType.size() == 1) {
            param = ownType.get(0);
        }
 
        return  param;
 
    }
 
 
    /**
    * @Description: 新增配置方案
    * @date 2022/2/11 16:17
    */
    @Override
    public void addOrUpdateControlParams(ControlPrintParam controlPrintParam,UserInfo userInfo) {
 
        Long id = controlPrintParam.getId();
        if (id == null) {
            controlPrintParam.setOwner(userInfo.getUsername());
            this.save(controlPrintParam);
        } else{
            if (userInfo.getUsername().equals(controlPrintParam.getOwner())) {
                this.updateById(controlPrintParam);
            }else{
                if (userInfo.getType() != 1)
                    controlPrintParam.setOwner(userInfo.getUsername());
                this.save(controlPrintParam);
            }
        }
 
 
    }
 
    /**
    * @Description: 更新配置方案
    * @date 2022/2/11 17:30
    */
    @Override
    public void updateControlParams(ControlPrintParam controlPrintParam, UserInfo user) {
 
        Long id = controlPrintParam.getId();
        if (id == null) throw new BusinessException("参数不能为空");
        controlPrintParam.setOwner(null);
        controlPrintParam.setType(null);
        this.updateById(controlPrintParam);
    }
 
}