package com.lz.modules.equipment.controller;


import com.lz.common.utils.PageUtils;
import com.lz.common.utils.R;
import com.lz.modules.app.utils.BeanUtils;
import com.lz.modules.equipment.entity.EquipmentType;
import com.lz.modules.equipment.entity.EquipmentTypeReq;
import com.lz.modules.equipment.service.IEquipmentInfoService;
import com.lz.modules.equipment.service.IEquipmentTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 类型表
 * @author quyixiao
 * @version 1.0
 * @since 2020年07月29日 16时48分23秒
 */
@RestController
public class EquipmentTypeController {



	@Resource
	private IEquipmentTypeService equipmentTypeService;

    @Resource
    private IEquipmentInfoService equipmentInfoService;

	
    /**
     * 添加类型表页面
     * 
     * @return 添加页面
     * @throws Exception 异常
     */
    @RequestMapping("/modules/type/equipmentTypeAddPage")
    public R equipmentTypeAddPage() throws Exception {

        return R.ok();
    }

    /**
     * 添加类型表
     * 
     * @throws Exception 异常
     */
    @RequestMapping("/modules/type/equipmentTypeAdd")
    @RequiresPermissions("user:device:type:save")
    public R equipmentTypeAdd(@RequestBody EquipmentTypeReq req) throws Exception {
        EquipmentType equipmentType = new EquipmentType();
        BeanUtils.copyProperty(equipmentType,req,true);
        equipmentTypeService.insertEquipmentType(equipmentType);
        return R.ok("添加成功");
    }


    /**
     * 更新类型表页面
     * @return 返回页面
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/type/equipmentTypeEditPage")
    public R equipmentTypeEditPage(@RequestBody EquipmentTypeReq req) throws Exception {
        EquipmentType equipmentType = equipmentTypeService.selectEquipmentTypeById(req.getId());
        return R.ok().put("equipmentType", equipmentType);
    }


    /**
     * 更新类型表
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/type/equipmentTypeEdit")
    @RequiresPermissions("user:device:type:update")
    public R equipmentTypeEdit(@RequestBody EquipmentTypeReq req) throws Exception {
        EquipmentType equipmentType = equipmentTypeService.selectEquipmentTypeById(req.getId());
        BeanUtils.copyProperty(equipmentType,req,true);
        equipmentTypeService.updateEquipmentTypeById(equipmentType);
        //更新所有再记录中所有的类型名称
        equipmentInfoService.updateCoverEquipmentInfoByTypeId(equipmentType.getId(), equipmentType.getType());
        return R.ok("编辑成功");
    }

    /**
     * 删除类型表
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/type/equipmentTypeDelete")
    @RequiresPermissions("user:device:type:delete")
    public R equipmentTypeDelete(@RequestBody EquipmentTypeReq req) throws Exception {
        equipmentTypeService.deleteEquipmentTypeById(req.getId());
        return R.ok("删除成功");
    }

    /**
     * 获取类型表集合
     * @return 返回页面
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/type/equipmentTypeManager")
    public R equipmentTypeManager() throws Exception {
        return R.ok();
    }

    /**
     * 获取类型表集合
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/type/equipmentTypeList")
    @RequiresPermissions("user:device:type:list")
    public R equipmentTypeList(@RequestBody EquipmentTypeReq req) throws Exception {
        PageUtils page = equipmentTypeService.selectByCondition(req);
        return R.ok().put("total", page.getTotalCount())
                .put("rows", page.getList());
    }
}
