1
935090232@qq.com
2020-12-01 611146e69aaa62296cf84f2ccb5aca5ebba17677
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
package com.matrix.system.shopXcx.action;
 
import com.matrix.core.anotations.RemoveRequestToken;
import com.matrix.core.anotations.SaveRequestToken;
import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.constance.SystemMessageCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.WebUtil;
import com.matrix.system.common.bean.SysUsers;
import com.matrix.system.common.constance.AppConstance;
import com.matrix.system.common.tools.ServiceUtil;
import com.matrix.system.hive.action.BaseController;
import com.matrix.system.hive.action.util.QueryUtil;
import com.matrix.system.shopXcx.bean.ShopProductAttribute;
import com.matrix.system.shopXcx.dao.ShopProductAttributeDao;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
 
import java.util.*;
 
/**
 * @description 商品属性管理
 * @author pengliang
 * @date 2019-06-04 16:10
 */
@Controller
@RequestMapping(value = "admin/shopProductAttribute")
public class ShopProductAttributeAction extends BaseController {
 
    @Autowired
    private ShopProductAttributeDao shopProductAttributeDao;
 
    @Autowired
    private ServiceUtil serviceUtil;
    
    //记录编辑前的值Before_Edit_Value
    public static final String BEV="ShopProductAttribute_BEV";
    
    
    /**
     * 列表显示
     */
    @RequestMapping(value =  "/showList")
    public @ResponseBody AjaxResult showList(ShopProductAttribute shopProductAttribute, PaginationVO pageVo) {
 
        List<ShopProductAttribute> dataList = shopProductAttributeDao.selectInPage(shopProductAttribute, pageVo);
        AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, dataList,
                shopProductAttributeDao.selectTotalRecord(shopProductAttribute));
        return result;
    }
 
    //显示所有属性
    @RequestMapping(value = "/all")
    @ResponseBody
    public AjaxResult showAll(ShopProductAttribute shopProductAttribute){
//        SysUsers sysUsers = getMe();
//        shopProductAttribute.setShopId(sysUsers.getShopId());
        QueryUtil.setQueryLimitCom(shopProductAttribute);
        List<ShopProductAttribute> list = shopProductAttributeDao.selectByModel(shopProductAttribute);
        return  new AjaxResult(AjaxResult.STATUS_SUCCESS,list);
    }
 
    /**
     * 过滤属性
     * @param attrCode
     * @return
     */
    @RequestMapping(value = "/showSonAttribute")
    @ResponseBody
    public AjaxResult showSonAttribute(String attrCode,String status){
        SysUsers sysUsers = getMe();
        ShopProductAttribute shopProductAttribute = shopProductAttributeDao.selectByIdAsTree(attrCode,sysUsers.getCompanyId());
        if(null == shopProductAttribute){
            return new AjaxResult(AjaxResult.STATUS_FAIL,"未找到属性");
        }
        if(StringUtils.isNotBlank(status)){
            List<ShopProductAttribute> list = new ArrayList<>();
            getList(list, shopProductAttribute);
            list.remove(shopProductAttribute);
            return new AjaxResult(AjaxResult.STATUS_SUCCESS,list);
        }else{
            List<ShopProductAttribute> list = new ArrayList<>();
            getList(list, shopProductAttribute);
            ShopProductAttribute productAttribute = new ShopProductAttribute();
            productAttribute.setShopId(sysUsers.getShopId());
            List<ShopProductAttribute> allAttributeList = shopProductAttributeDao.selectByModel(productAttribute);
            Iterator<ShopProductAttribute> iterator = allAttributeList.iterator();
            while (iterator.hasNext()){
                ShopProductAttribute attribute = iterator.next();
                if(!list.contains(attribute)){
                    iterator.remove();
                }
                if(AppConstance.PARENTID_ATTRIBUTE.equals(attribute.getParentId()) && list.contains(attribute)){
                    iterator.remove();
                }
            }
            return new AjaxResult(AjaxResult.STATUS_SUCCESS,allAttributeList);
        }
 
    }
 
    /**
     * 递归调用获取子节点
     * @param sourceList
     * @param targetAttr
     */
    public void  getList(List<ShopProductAttribute> sourceList,ShopProductAttribute targetAttr){
        sourceList.add(targetAttr);
        if(targetAttr != null && CollectionUtils.isNotEmpty(targetAttr.getChild())){
            for (ShopProductAttribute childAttr:targetAttr.getChild() ) {
                    getList(sourceList,childAttr);
                }
        }
    }
 
    /**
     * 新增
     */   
    @RemoveRequestToken    
       @RequestMapping(value =  "/addShopProductAttribute")
    public @ResponseBody AjaxResult addShopProductAttribute(ShopProductAttribute shopProductAttribute) {
        SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
        shopProductAttribute.setCreateBy(user.getSuName());
        shopProductAttribute.setUpdateBy(user.getSuName());
        shopProductAttribute.setShopId(user.getShopId());
        shopProductAttribute.setCompanyId(user.getCompanyId());
        boolean isRepetition = serviceUtil.addCheckRepeatTowColumn("shop_product_attribute",
                "attr_code", shopProductAttribute.getAttrCode(),
                "company_id",shopProductAttribute.getCompanyId());
        if(isRepetition){
            return  new AjaxResult(AjaxResult.STATUS_FAIL,"属性编码不能重复");
        }
        shopProductAttribute.setShopId(user.getShopId());
        int i=shopProductAttributeDao.insert(shopProductAttribute);
         if(i > 0){
             return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.ADD_SUCCES, "商品属性管理");
         }else {
            throw new GlobleException(SystemErrorCode.DATA_ADD_FAIL);
        }
    }
    
    @RequestMapping(value = "/findById")
    @ResponseBody
    public AjaxResult findById(ShopProductAttribute shopProductAttribute){
        shopProductAttribute = shopProductAttributeDao.selectById(shopProductAttribute.getAttrId());
        return new AjaxResult(AjaxResult.STATUS_SUCCESS, Arrays.asList(shopProductAttribute));
    }
    
    
    /**
     * 修改
     */   
    @RemoveRequestToken    
       @RequestMapping(value =  "/modifyShopProductAttribute")
    public @ResponseBody AjaxResult modifyShopProductAttribute(ShopProductAttribute shopProductAttribute) {
        SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
        shopProductAttribute.setUpdateBy(user.getSuName());
        shopProductAttribute.setUpdateTime(new Date());
        ShopProductAttribute newShopProductAttribute = shopProductAttributeDao.selectById(shopProductAttribute.getAttrId());
        boolean isRepetition;
        if(shopProductAttribute.getAttrCode().equals(newShopProductAttribute.getAttrCode())){
            isRepetition = serviceUtil.updateCheckRepeatTowColumn("shop_product_attribute", "attr_code", newShopProductAttribute.getAttrCode()
                    ,"company_id", newShopProductAttribute.getCompanyId()
                    ,"attr_code",shopProductAttribute.getAttrCode());
        }else{
            isRepetition = serviceUtil.addCheckRepeatTowColumn("shop_product_attribute",
                    "attr_code", shopProductAttribute.getAttrCode(),
                    "company_id",shopProductAttribute.getCompanyId());
        }
 
        if(isRepetition){
            return  new AjaxResult(AjaxResult.STATUS_FAIL,"属性编码不能重复");
        }
        int i = shopProductAttributeDao.updateByModel(shopProductAttribute);
        if(i>0){
            return  new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.UPDATE_SUCCES, "商品属性管理");
        }else{
            throw new GlobleException(SystemErrorCode.DATA_ADD_FAIL);
        }
 
    }
    
    
    
    
       /**
     * 进入修改界面
     */   
    @SaveRequestToken
       @RequestMapping(value =  "/editForm")
    public ModelAndView editForm(Integer id) {
        SysUsers sysUsers = getMe();
        ShopProductAttribute shopProductAttribute = new ShopProductAttribute();
        shopProductAttribute.setShopId(sysUsers.getShopId());
        List<ShopProductAttribute> list = shopProductAttributeDao.selectByModel(shopProductAttribute);
        ModelAndView modelAndView = new ModelAndView("admin/shop/shopProductAttribute-form");
        modelAndView.addObject("obj",list);
        return modelAndView;
    }
       
       
       /**
     * 删除
     */  
     @RequestMapping(value = "/del")
    public @ResponseBody AjaxResult del(Integer attrId) {
        String id = shopProductAttributeDao.selectForById(attrId);
        List<String> ids = new ArrayList<String>();
        if(StringUtils.isNotBlank(id)){
            ids = StringUtils.strToCollToString(id, ",");
        }
        ids.add(String.valueOf(attrId));
        int i =  shopProductAttributeDao.deleteByIds(ids);
        if (i > 0) {
            return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.DELETE_SUCCES, i);
        } else {
            throw new GlobleException(SystemErrorCode.DATA_DELETE_FAIL);
        }
    }
 
    /**
     * 查询所有属性
     */
    @RequestMapping(value = "/getAll")
    public @ResponseBody AjaxResult getAll() {
        SysUsers sysUsers = getMe();
        ShopProductAttribute shopProductAttribute = new ShopProductAttribute();
        shopProductAttribute.setShopId(sysUsers.getShopId());
        shopProductAttribute.setCompanyId(sysUsers.getCompanyId());
        List<ShopProductAttribute> attrList=  shopProductAttributeDao.selectByModel(shopProductAttribute);
        return new AjaxResult(AjaxResult.STATUS_SUCCESS, attrList, attrList.size());
    }
 
}