package com.platform.controller;

import com.platform.annotation.CanUserUpdateRecord;
import com.platform.entity.ProductEntity;
import com.platform.entity.Result;
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.web.bind.annotation.*;

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

/**
 * Controller
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-08-30 14:31:21
 */
@RestController
@RequestMapping("product")
public class ProductController extends AbstractController{
    @Autowired
    private ProductService productService;


    @Autowired
    private MySysUserBrandService mySysUserBrandService;

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

        List<Long> roleIdList = getUser().getRoleIdList();
        if (!roleIdList.contains(1L) && !roleIdList.contains(5L)) {
            query.put("brandIdList", mySysUserBrandService.queryBrandIdList(getUserId()));
        }

        List<ProductEntity> productList = productService.queryList(query);
        int total = productService.queryTotal(query);

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

        return Result.success(pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    
    public Result info(@PathVariable("id") Integer id) {
        ProductEntity product = productService.queryObject(id);

        return Result.success( product);
    }

    /**
     * 保存
     */
    @CanUserUpdateRecord
    @RequestMapping("/save")
    
    public Result save(@RequestBody ProductEntity product) {
        productService.save(product);

        return Result.success();
    }

    /**
     * 修改
     */
    @CanUserUpdateRecord
    @RequestMapping("/update")
    
    public Result update(@RequestBody ProductEntity product) {
        productService.update(product);

        return Result.success();
    }

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

        return Result.success();
    }

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

        List<ProductEntity> list = productService.queryList(params);

        return Result.success( list);
    }

    /**
     * 根据goodsId查询商品
     *
     * @param goodsId
     * @return
     */
    @RequestMapping("/queryByGoodsId/{goodsId}")
    public Result queryByGoodsId(@PathVariable("goodsId") String goodsId) {
        Map<String, Object> params = new HashMap<>();
        params.put("goodsId", goodsId);
        List<ProductEntity> list = productService.queryList(params);

        return Result.success( list);
    }
}
