package com.platform.controller;

import com.platform.annotation.CanUserUpdateRecord;
import com.platform.entity.GoodsSpecificationEntity;
import com.platform.entity.Result;
import com.platform.entity.SpecificationEntity;
import com.platform.service.GoodsSpecificationService;
import com.platform.service.SpecificationService;
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;


/**
 * 规格表
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-08-13 10:41:10
 */
@RestController
@RequestMapping("/specification")
public class SpecificationController {
    @Autowired
    private SpecificationService specificationService;

    @Autowired
    private GoodsSpecificationService goodsSpecificationService;

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

        List<SpecificationEntity> specificationList = specificationService.queryList(query);
        int total = specificationService.queryTotal(query);

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

        return Result.success( pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    
    public Result info(@PathVariable("id") Integer id) {
        SpecificationEntity specification = specificationService.queryObject(id);

        return Result.success( specification);
    }

    /**
     * 保存
     */
    @CanUserUpdateRecord
    @RequestMapping("/save")
    
    public Result save(@RequestBody SpecificationEntity specification) {
        specificationService.save(specification);

        return Result.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    
    public Result update(@RequestBody SpecificationEntity specification) {
        specificationService.update(specification);

        return Result.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    
    public Result delete(@RequestBody Integer[] ids) {
        Map<String, Object> map = new HashMap<>();
        for(Integer id : ids){
            map.put("specificationId", id);
            List<GoodsSpecificationEntity> list = goodsSpecificationService.queryList(map);
            if(list != null && list.size() > 0){
                return Result.failure("该属性已绑定商品！");
            }
        }
        specificationService.deleteBatch(ids);

        return Result.success();
    }

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

        List<SpecificationEntity> list = specificationService.queryList(params);

        return Result.success(list);
    }
}
