package com.lz.modules.equipment.controller;

import com.lz.common.utils.NumberUtil;
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.*;
import com.lz.modules.equipment.service.EquipmentBrandService;
import com.lz.modules.equipment.service.IEquipmentInfoService;
import com.lz.modules.equipment.service.IEquipmentTypeService;
import com.lz.modules.equipment.service.TCountService;
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;
import java.util.List;

/**
 * 设备表
 *
 * @author quyixiao
 * @version 1.0
 * @since 2020年07月29日 17时40分17秒
 */
@RestController
public class EquipmentInfoController {


    @Resource
    private IEquipmentInfoService equipmentInfoService;

    @Autowired
    private IEquipmentTypeService equipmentTypeService;

    @Autowired
    private EquipmentBrandService equipmentBrandService;

    @Autowired
    private TCountService tCountService;



    /**
     * 添加设备表页面
     *
     * @return 添加页面
     * @throws Exception 异常
     */
    @RequestMapping("/modules/usereq/equipmentInfoAddPage")
    public R equipmentInfoAddPage() throws Exception {
        List<EquipmentType> types = equipmentTypeService.selectAll();
        List<EquipmentBrand> brands = equipmentBrandService.selectByTypeId(types.get(0).getId());
        return R.ok().put("types", types)
                .put("brands", brands);
    }

    /**
     * 添加设备表
     *
     * @throws Exception 异常
     */
    @RequestMapping("/modules/usereq/equipmentInfoAdd")
    public R equipmentInfoAdd(@RequestBody EquipmentInfoReq req) throws Exception {
        EquipmentInfo equipmentInfo = new EquipmentInfo();
        BeanUtils.copyProperty(equipmentInfo, req, true);

        Long typeId = NumberUtil.objToLongDefault(equipmentInfo.getType(), 0);
        EquipmentType type = equipmentTypeService.selectEquipmentTypeById(typeId);
        equipmentInfo.setType(type.getType());

        Long brandId = NumberUtil.objToLongDefault(equipmentInfo.getBrandName(), 0);
        EquipmentBrand equipmentBrand = equipmentBrandService.selectEquipmentBrandById(brandId);
        equipmentInfo.setBrandName(equipmentBrand.getBrand());

        equipmentInfoService.insertEquipmentInfo(equipmentInfo);
        return R.ok("添加成功");
    }

    /**
     * 更新设备表页面
     *
     * @return 返回页面
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/usereq/equipmentInfoEditPage")
    public R equipmentInfoEditPage(@RequestBody EquipmentInfoReq req) throws Exception {
        EquipmentInfo equipmentInfo = equipmentInfoService.selectEquipmentInfoById(req.getId());
        EquipmentType type = equipmentTypeService.selectByType(equipmentInfo.getType());
        List<EquipmentType> types = equipmentTypeService.selectAll();
        List<EquipmentBrand> brands = equipmentBrandService.selectByTypeId(type.getId());
        return R.ok().put("equipmentInfo", equipmentInfo)
                .put("types", types)
                .put("brands", brands);
    }


    /**
     * 更新设备表
     *
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/usereq/equipmentInfoEdit")
    @RequiresPermissions("user:device:update")
    public R equipmentInfoEdit(@RequestBody EquipmentInfoReq req) throws Exception {
        EquipmentInfo equipmentInfo = equipmentInfoService.selectEquipmentInfoById(req.getId());
        BeanUtils.copyProperty(equipmentInfo, req, true);
        /*Long typeId = NumberUtil.objToLongDefault(equipmentInfo.getType(), 0);//20200921注释掉，后台不支持修改类型，品牌等，只能修改备注，时间，金额
        EquipmentType type = equipmentTypeService.selectEquipmentTypeById(typeId);
        equipmentInfo.setType(type.getType());

        Long brandId = NumberUtil.objToLongDefault(equipmentInfo.getBrandName(), 0);
        EquipmentBrand equipmentBrand = equipmentBrandService.selectEquipmentBrandById(brandId);
        equipmentInfo.setBrandName(equipmentBrand.getBrand());*/
        equipmentInfoService.updateEquipmentInfoById(equipmentInfo);
        return R.ok("编辑成功");
    }

    /**
     * 删除设备表
     *
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/usereq/equipmentInfoDelete")
    @RequiresPermissions("user:device:delete")
    public R equipmentInfoDelete(@RequestBody EquipmentInfoReq req) throws Exception {
        equipmentInfoService.deleteEquipmentInfoById(req.getId());
        return R.ok("删除成功");
    }




    /**
     * 获取设备表集合
     *
     * @return 返回页面
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/usereq/equipmentInfoManager")
    public R equipmentInfoManager() throws Exception {
        List<EquipmentType> types = equipmentTypeService.selectAll();
        List<TCount> tCounts = tCountService.selectListLimit(10);
        return R.ok().put("types", types)
                .put("tCounts",tCounts);
    }

    /**
     * 获取设备表集合
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/usereq/equipmentInfoList")
    @RequiresPermissions("user:device:list")
    public R equipmentInfoList(@RequestBody EquipmentInfoReq req) throws Exception {
        List<EquipmentType> types = equipmentTypeService.selectAll();
        List<TCount> tcounts = tCountService.selectListLimit(100);
        EquipmentType type = new EquipmentType();
        type.setId(null);
        type.setType("全部");
        types.add(0, type);
        PageUtils page = equipmentInfoService.selectByCondition(req);
        return R.ok().put("total", page.getTotalCount())
                .put("rows", page.getList())
                .put("types", types)
                .put("tCounts", tcounts)
                .put("curCount", req.getCount());
    }
}
