package com.kmall.admin.controller;

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

import com.kmall.admin.entity.SupplierEntity;
import com.kmall.admin.service.SupplierService;
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.*;

/**
 * Controller
 *
 * @author emato
 * @email admin@qhdswl.com
 * @date 2018-11-26 15:14:24
 */
@Controller
@RequestMapping("supplier")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("supplier:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", "thirdPartyMerchCode");
        String name = (String) params.get("name");
        if(org.apache.commons.lang3.StringUtils.isNotEmpty(name)){
            try{
                name = new String(name.getBytes("iso-8859-1"),"utf-8");
            }catch (Exception e){
                e.printStackTrace();
            }
            params.put("name", name);
        }
        //查询列表数据
        Query query = new Query(params);

        List<SupplierEntity> supplierList = supplierService.queryList(query);
        int total = supplierService.queryTotal(query);

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

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

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("supplier:info")
    @ResponseBody
    public R info(@PathVariable("id") Integer id) {
        SupplierEntity supplier = supplierService.queryObject(id);
        return R.ok().put("supplier", supplier);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("supplier:save")
    @ResponseBody
    public R save(@RequestBody SupplierEntity supplier) {
        supplierService.save(supplier);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("supplier:update")
    @ResponseBody
    public R update(@RequestBody SupplierEntity supplier) {
        supplierService.update(supplier);

        return R.ok();
    }

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

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {
        ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", "thirdPartyMerchCode");
        List<SupplierEntity> list = supplierService.queryList(params);

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