package io.renren.modules.sys.controller;

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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.SysUnitEntity;
import io.renren.modules.sys.service.SysUnitService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * ${comments}
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-12-25 15:57:17
 */
@RestController
@RequestMapping("sys/sysunit")
public class SysUnitController {
    @Autowired
    private SysUnitService sysUnitService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:sysunit:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = sysUnitService.queryPage(params);

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

    /**
     * 首页
     */
    @RequestMapping(value = "unitList")
    public List<SysUnitEntity> unitList(@RequestBody Map<String, Object> params) {
        List plantIds = (List) params.get("plantIds");
        List<SysUnitEntity> list = sysUnitService.list(new QueryWrapper<SysUnitEntity>()
                .in("PLANT_NAME", plantIds)
        );
        return list;
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{unitId}")
    @RequiresPermissions("sys:sysunit:info")
    public R info(@PathVariable("unitId") Integer unitId){
		SysUnitEntity sysUnit = sysUnitService.getById(unitId);

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

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:sysunit:save")
    public R save(@RequestBody SysUnitEntity sysUnit){
		sysUnitService.save(sysUnit);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:sysunit:update")
    public R update(@RequestBody SysUnitEntity sysUnit){
		sysUnitService.updateById(sysUnit);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:sysunit:delete")
    public R delete(@RequestBody Integer[] unitIds){
		sysUnitService.removeByIds(Arrays.asList(unitIds));

        return R.ok();
    }

}
