package com.platform.controller;

import com.platform.annotation.CanUserUpdateRecord;
import com.platform.entity.GoodsSpecificationEntity;
import com.platform.entity.ProductEntity;
import com.platform.entity.Result;
import com.platform.service.GoodsSpecificationService;
import com.platform.service.ProductService;
import com.platform.service.custom.MySysUserBrandService;
import com.platform.utils.Constant;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 商品对应规格表值表Controller
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-08-31 11:15:55
 */
@RestController
@RequestMapping("goodsspecification")
public class GoodsSpecificationController extends AbstractController{
    @Autowired
    private GoodsSpecificationService goodsSpecificationService;

    @Autowired
    private ProductService productService;



    @Autowired
    private MySysUserBrandService mySysUserBrandService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    
    public Result list(@RequestBody Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        Long userId = getUserId();
        logger.info("系统用户id: {}", userId);
        List<Long> roleIdList = getUser().getRoleIdList();
        if (!roleIdList.contains(1L) && !roleIdList.contains(5L)) {
            query.put("brandIdList", mySysUserBrandService.queryBrandIdList(userId));
        }

        List<GoodsSpecificationEntity> goodsSpecificationList = goodsSpecificationService.queryList(query);
        int total = goodsSpecificationService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(goodsSpecificationList, total, query.getLimit(), query.getPage());

        return Result.success( pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    
    public Result info(@PathVariable("id") Integer id) {
        GoodsSpecificationEntity goodsSpecification = goodsSpecificationService.queryObject(id);

        return Result.success( goodsSpecification);
    }

    /**
     * 保存
     */
    @CanUserUpdateRecord
    @RequestMapping("/save")
    
    public Result save(@RequestBody GoodsSpecificationEntity goodsSpecification) {
        goodsSpecificationService.save(goodsSpecification);

        ProductEntity entity = new ProductEntity();
        entity.setGoodsId(goodsSpecification.getGoodsId());
        ProductEntity productEntity = productService.queryObjectByGoodsId(entity);
        if(productEntity != null ){
            if(!StringUtils.isEmpty(productEntity.getGoodsSpecificationIds())){
                productEntity.setGoodsSpecificationIds(productEntity.getGoodsSpecificationIds() + "_" + goodsSpecification.getId());
            }else {
                productEntity.setGoodsSpecificationIds(String.valueOf(goodsSpecification.getId()));
            }
            productService.update(productEntity);
        }

        return Result.success();
    }

    /**
     * 修改
     */
    @CanUserUpdateRecord
    @RequestMapping("/update")
    
    public Result update(@RequestBody GoodsSpecificationEntity goodsSpecification) {
        goodsSpecificationService.update(goodsSpecification);

        return Result.success();
    }

    /**
     * 删除
     */
    @CanUserUpdateRecord
    @RequestMapping("/delete")
    
    public Result delete(@RequestBody Integer[]ids) {
        goodsSpecificationService.deleteBatch(ids);

        return Result.success();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public Result queryAll(@RequestBody Map<String, Object> params) {

        List<GoodsSpecificationEntity> list = goodsSpecificationService.queryList(params);

        return Result.success( list);
    }
}
