package com.bomaos.common.system.controller;

import com.bomaos.common.core.annotation.OperLog;
import com.bomaos.common.core.web.*;
import com.bomaos.common.system.entity.Supplier;
import com.bomaos.common.system.service.SupplierService;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 供应商管理
 */
@Controller
@RequestMapping("/sys/supplier")
public class SupplierController extends BaseController {

    @Autowired
    private SupplierService supplierService;

    @RequiresPermissions("sys:supplier:view")
    @RequestMapping()
    public String view() {
        return "system/supplier.html";
    }

    /**
     * 分页查询供应商
     */
    @RequiresPermissions("sys:supplier:list")
    @OperLog(value = "供应商管理", desc = "分页查询")
    @ResponseBody
    @RequestMapping("/page")
    public PageResult<Supplier> page(HttpServletRequest request) {
        PageParam<Supplier> pageParam = new PageParam<>(request);
        pageParam.setDefaultOrder(null, new String[]{"create_time"});
        return supplierService.listPage(pageParam);
    }

    /**
     * 查询全部供应商
     */
    @RequiresPermissions("sys:supplier:list")
    @OperLog(value = "供应商管理", desc = "查询全部")
    @ResponseBody
    @RequestMapping("/list")
    public JsonResult list(HttpServletRequest request) {
        PageParam<Supplier> pageParam = new PageParam<>(request);
        List<Supplier> records = supplierService.listAll(pageParam.getNoPageParam());
        return JsonResult.ok().setData(pageParam.sortRecords(records));
    }

    /**
     * 根据id查询供应商
     */
    @RequiresPermissions("sys:supplier:list")
    @OperLog(value = "供应商管理", desc = "根据id查询")
    @ResponseBody
    @RequestMapping("/get")
    public JsonResult get(Integer id) {
        return JsonResult.ok().setData(supplierService.getById(id));
    }

    /**
     * 添加供应商
     */
    @RequiresPermissions("sys:supplier:save")
    @OperLog(value = "供应商管理", desc = "添加", param = false, result = true)
    @ResponseBody
    @RequestMapping("/save")
    public JsonResult save(Supplier supplier) {
        supplier.setCreateTime(new Date());
        supplier.setUpdateTime(new Date());
        if (supplierService.save(supplier)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    /**
     * 修改供应商
     */
    @RequiresPermissions("sys:supplier:update")
    @OperLog(value = "供应商管理", desc = "修改", param = false, result = true)
    @ResponseBody
    @RequestMapping("/update")
    public JsonResult update(Supplier supplier) {
        supplier.setUpdateTime(new Date());
        if (supplierService.updateById(supplier)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    /**
     * 删除供应商
     */
    @RequiresPermissions("sys:supplier:remove")
    @OperLog(value = "供应商管理", desc = "删除", result = true)
    @ResponseBody
    @RequestMapping("/remove")
    public JsonResult remove(Integer id) {
        if (supplierService.removeById(id)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

    /**
     * 批量删除供应商
     */
    @RequiresPermissions("sys:supplier:remove")
    @OperLog(value = "供应商管理", desc = "批量删除", result = true)
    @ResponseBody
    @RequestMapping("/removeBatch")
    public JsonResult removeBatch(@RequestBody List<Integer> ids) {
        if (supplierService.removeByIds(ids)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

}
