luqingyang
2023-10-24 cb642724c54d7d850aec5e5ee27fcc7186f352d7
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
package com.consum.base.service;
 
import com.consum.base.Constants;
import com.consum.base.core.CodeGeneratorService;
import com.consum.base.pojo.BaseGoodsTemplateParam;
import com.consum.base.util.IdUtil;
import com.consum.model.po.BaseCategory;
import com.consum.model.po.BaseGoodsModels;
import com.consum.model.po.BaseGoodsTemplate;
import com.consum.model.vo.BaseGoodsTemplateVo;
import com.iplatform.model.po.S_user_core;
import com.walker.db.page.GenericPager;
import com.walker.infrastructure.utils.DateUtils;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.jdbc.service.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.HashMap;
import java.util.List;
 
/**
 * @Description 物品模板
 * @Author 卢庆阳
 * @Date 2023/10/23
 */
@Service
public class BaseGoodsTemplateServiceImpl extends BaseServiceImpl {
 
    @Autowired
    private CodeGeneratorService codeGeneratorService;
    @Autowired
    private BaseCategoryServiceImpl baseCategoryService;
    @Autowired
    private BaseGoodsModelsServiceImpl baseGoodsModelsService;
 
    /**
     * @return
     * @Description 根据分类id和状态查询物品模版
     * @Author 卢庆阳
     * @Date 2023/10/23
     */
    public List<BaseCategory> getByCategoryId(Long categoryId) {
        StringBuilder sql = new StringBuilder("SELECT * FROM base_goods_template WHERE 1 = 1 ");
        HashMap<String, Object> paramts = new HashMap<>();
 
        //分类id
        if (categoryId != null) {
            sql.append(" and category_id =:category_id ");
            paramts.put("category_id", categoryId);
        }
        sql.append(" and states =1 ");
 
        return this.select(sql.toString(), paramts, new BaseCategory());
    }
 
    /**
     * @Description 新增物品模板
     * @Author 卢庆阳
     * @Date 2023/10/24
     */
    public int add(BaseGoodsTemplateParam param, S_user_core currentUser) {
        //1.新增物品模板
        BaseGoodsTemplate baseGoodsTemplate = new BaseGoodsTemplate();
        BeanUtils.copyProperties(param, baseGoodsTemplate);
        long goodsTemplatesId = IdUtil.generateId();
        baseGoodsTemplate.setId(goodsTemplatesId);
        //NumberGenerator.getLongSequenceNumber()
        //根据分类id查询分类
        BaseCategory baseCategory = this.baseCategoryService.get(new BaseCategory(param.getCategoryId()));
        if (baseCategory != null) {
            String classification = baseCategory.getClassification();
            //物品编码
            String goodsCode = codeGeneratorService.createGoodsTemplateCode(classification);
            baseGoodsTemplate.setGoodsCode(goodsCode);
            //类别
            baseGoodsTemplate.setClassification(classification);
        }
        //TODO 一级二级三级机构编号
        //当前机构的父级机构编号
        //机构层级
        //机构编号
        //机构名称
        //创建时间
        long createTime = DateUtils.getDateTimeNumber(System.currentTimeMillis());
        baseGoodsTemplate.setCreateDate(createTime);
        int flag1 = this.insert(baseGoodsTemplate);
 
        //2.新增规格型号
        List<BaseGoodsModels> models = param.getModels();
        int flag2 = 0;
        if (!CollectionUtils.isEmpty(models)) {
            for (BaseGoodsModels model : models) {
                model.setId(IdUtil.generateId());
                model.setStates(Constants.STATES_ENABLE);
                model.setGoodsTemplatesId(goodsTemplatesId);
                model.setCreateTime(createTime);
            }
            flag2 = this.baseGoodsModelsService.insert(models);
        }
 
        if (flag1 > 0 && flag2 > 0) {
            return 1;
        } else {
            return 0;
        }
    }
 
    /**
     * @Description  物品模板列表查询
     * @Author 卢庆阳
     * @Date 2023/10/24
     */
    public GenericPager<BaseGoodsTemplate> queryList(BaseGoodsTemplateParam param) {
        StringBuilder sql = new StringBuilder("SELECT * FROM base_goods_template WHERE 1 = 1 ");
        HashMap<String, Object> paramts = new HashMap<>();
 
        //物品名称
        if (!StringUtils.isEmpty(param.getGoodsName())) {
            sql.append(" and goods_name like:goods_name ");
            paramts.put("goods_name", StringUtils.CHAR_PERCENT + param.getGoodsName() + StringUtils.CHAR_PERCENT);
        }
        //物品编号
        if (!StringUtils.isEmpty(param.getGoodsCode())) {
            sql.append(" and goods_code like:goods_code ");
            paramts.put("goods_code", StringUtils.CHAR_PERCENT + param.getGoodsCode() + StringUtils.CHAR_PERCENT);
        }
        //分类id
        if (param.getCategoryId() != null) {
            sql.append(" and category_id =:category_id ");
            paramts.put("category_id", param.getCategoryId());
        }
        //类别
        if (!StringUtils.isEmpty(param.getClassification())) {
            sql.append(" and classification =:classification ");
            paramts.put("classification", param.getClassification());
        }
        //所属机构
        if (param.getAgencyId() != null) {
            sql.append(" and agency_id =:agency_id ");
            paramts.put("agency_id", param.getAgencyId());
        }
        //状态
        if (param.getStates() != null) {
            sql.append(" and states =:states ");
            paramts.put("states", param.getStates());
        }
        sql.append(" ORDER BY CREATE_DATE DESC ");
 
        return selectSplit(sql.toString(), paramts, new BaseGoodsTemplate());
    }
 
    /**
     * 根据物品id查询物品详情
     * @author 卢庆阳
     * @date 2023/9/26
     */
    public BaseGoodsTemplateVo getById(Long id) {
        //1.根据物品id查询物品模板
        BaseGoodsTemplate goodsTemplate = this.get(new BaseGoodsTemplate(id));
        //2.根据物品id查询规格型号
        StringBuilder sql = new StringBuilder("SELECT * FROM base_goods_models WHERE 1 = 1 ");
        HashMap<String, Object> paramts = new HashMap<>();
        //所属物品模版编号
        sql.append(" and goods_templates_id =:goods_templates_id ");
        paramts.put("goods_templates_id", id);
        List<BaseGoodsModels> modelsList = this.select(sql.toString(), paramts, new BaseGoodsModels());
 
        //3.封装
        BaseGoodsTemplateVo vo = new BaseGoodsTemplateVo();
        if (goodsTemplate != null) {
            BeanUtils.copyProperties(goodsTemplate,vo);
        }
        if (!CollectionUtils.isEmpty(modelsList)) {
            vo.setModels(modelsList);
        }
        return vo;
    }
 
    /**
     * @Description 编辑
     * @Author 卢庆阳
     * @Date 2023/10/24
     */
    public int updateBaseGoodsTemplate(BaseGoodsTemplateParam param) {
        //1.修改物品模板
        BaseGoodsTemplate baseGoodsTemplate = new BaseGoodsTemplate();
        BeanUtils.copyProperties(param, baseGoodsTemplate);
        int flag1 = this.update(baseGoodsTemplate);
        //2.修改规格型号的单位
        List<BaseGoodsModels> modelsList = param.getModels();
        int flag2 = this.update(modelsList);
 
        if (flag1 > 0 && flag2 > 0) {
            return 1;
        } else {
            return 0;
        }
    }
 
    /**
     * @Description  根据物品名称和分类id查询物品
     * @Author 卢庆阳
     * @Date 2023/10/24
     */
    public BaseGoodsTemplate getByGoodsNameAndCategoryId(String goodsName, Long categoryId) {
        StringBuilder sql = new StringBuilder("SELECT * FROM base_goods_template WHERE 1 = 1 ");
        HashMap<String, Object> paramts = new HashMap<>();
 
        //物品名称
        sql.append(" and goods_name =:goods_name ");
        paramts.put("goods_name", goodsName);
        //分类id
        sql.append(" and category_id =:category_id ");
        paramts.put("category_id", categoryId);
 
        return this.get(sql.toString(), paramts, new BaseGoodsTemplate());
    }
}