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()); 
 | 
    } 
 | 
  
 | 
} 
 |