package com.kmall.admin.controller;

import com.kmall.admin.entity.GoodsSpecificationEntity;
import com.kmall.admin.entity.SpecificationEntity;
import com.kmall.admin.service.GoodsSpecificationService;
import com.kmall.admin.service.SpecificationService;
import com.kmall.common.utils.PageUtils;
import com.kmall.common.utils.Query;
import com.kmall.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 商品对应规格表值表Controller
 *
 * @author Scott
 * @email
 * @date 2017-08-31 11:15:55
 */
@RestController
@RequestMapping("goodsspecification")
public class GoodsSpecificationController {
    @Autowired
    private GoodsSpecificationService goodsSpecificationService;
    @Autowired
    private SpecificationService specificationService;

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

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

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

        return R.ok().put("page", pageUtil);
    }

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

        return R.ok().put("goodsSpecification", goodsSpecification);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("goodsspecification:save")
    public R save(@RequestBody GoodsSpecificationEntity goodsSpecification) {
        goodsSpecificationService.save(goodsSpecification);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("goodsspecification:update")
    public R update(@RequestBody GoodsSpecificationEntity goodsSpecification) {
        goodsSpecificationService.update(goodsSpecification);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("goodsspecification:delete")
    public R delete(@RequestBody Integer[] ids) {
        goodsSpecificationService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {
        List<GoodsSpecificationEntity> list = goodsSpecificationService.queryList(params);
        List<Integer> goodsSpecificationIds = new ArrayList();
        if (null != list) {
            for (GoodsSpecificationEntity specificationEntity : list) {
                if (!goodsSpecificationIds.contains(specificationEntity.getSpecificationId())) {
                    goodsSpecificationIds.add(specificationEntity.getSpecificationId());
                }
            }
        }
        Map param = new HashMap();
        param.put("ids", goodsSpecificationIds);
        List<SpecificationEntity> specificationEntityList = specificationService.queryList(param);
        Collections.sort(specificationEntityList, new Comparator<SpecificationEntity>() {
            @Override
            public int compare(SpecificationEntity o1, SpecificationEntity o2) {
                return o1.getSortOrder().compareTo(o2.getSortOrder());
            }
        });
        return R.ok().put("list", list).put("specificationEntityList", specificationEntityList);
    }

}
