package com.kmall.admin.controller;

import com.kmall.admin.entity.ProductStoreRelaEntity;
import com.kmall.admin.entity.StoreEntity;
import com.kmall.admin.service.ProductStoreRelaService;
import com.kmall.admin.service.StoreService;
import com.kmall.admin.utils.ParamUtils;
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.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

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

/**
 * Controller
 *
 * @author Scott
 * @email
 * @date 2017-12-27 19:12:46
 */
@Controller
@RequestMapping("productstorerela")
public class ProductStoreRelaController {
    @Autowired
    private ProductStoreRelaService productStoreRelaService;
    @Autowired
    private StoreService storeService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", false);
        //查询列表数据
        Query query = new Query(params);

        List<ProductStoreRelaEntity> productStoreRelaList = productStoreRelaService.queryList(query);
        int total = productStoreRelaService.queryTotal(query);

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

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

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") Integer id) {
        ProductStoreRelaEntity productStoreRela = productStoreRelaService.queryObject(id);

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

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("productstorerela:save")
    @ResponseBody
    public R save(@RequestBody ProductStoreRelaEntity productStoreRela) {

        if (productStoreRela.getStoreId() != null) {
            StoreEntity store = storeService.queryObject(productStoreRela.getStoreId().intValue());
            if (store != null) {
                productStoreRela.setMerchSn(store.getMerchSn());
            }
        }
        productStoreRelaService.save(productStoreRela);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("productstorerela:update")
    @ResponseBody
    public R update(@RequestBody ProductStoreRelaEntity productStoreRela) {
        if (null == productStoreRela.getProductId() || null == productStoreRela.getGoodsId() ||
            null == productStoreRela.getStoreId()) {
            return R.error("信息不全，保存失败");
        }

        StoreEntity store = storeService.queryObject(productStoreRela.getStoreId().intValue());
        if (store != null) {
            productStoreRela.setMerchSn(store.getMerchSn());
        }

        productStoreRelaService.update(productStoreRela);
        return R.ok();
    }

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

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {
        ParamUtils.setQueryPowerByRoleType(params, "id", "merchSn", false);

        List<ProductStoreRelaEntity> list = productStoreRelaService.queryList(params);

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