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.EquipmentInfo;
import com.lz.modules.equipment.entity.TCount;
import com.lz.modules.equipment.entity.TCountReq;
import com.lz.modules.equipment.service.IEquipmentInfoService;
import com.lz.modules.equipment.service.TCountService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
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.ArrayList;

/**
 * 盘点
 *
 * @author quyixiao
 * @version 1.0
 * @since 2020年08月10日 09时10分47秒
 */
@RestController
public class TCountController {


    @Resource
    private TCountService tCountService;

    @Resource
    private IEquipmentInfoService equipmentInfoService;

    /**
     * 添加盘点页面
     *
     * @return 添加页面
     * @throws Exception 异常
     */
    @RequestMapping("/modules/count/tCountAddPage")
    public R tCountAddPage() throws Exception {

        return R.ok();
    }

    /**
     * 添加盘点
     *
     * @throws Exception 异常
     */
    @RequestMapping("/modules/count/tCountAdd")
    public R tCountAdd(@RequestBody TCountReq req) throws Exception {
        TCount tCount = new TCount();
        BeanUtils.copyProperty(tCount, req, true);
        tCountService.insertTCount(tCount);
        return R.ok("添加成功");
    }

    /**
     * 更新盘点页面
     *
     * @return 返回页面
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/count/tCountEditPage")
    public R tCountEditPage(@RequestBody TCountReq req) throws Exception {
        TCount tCount = tCountService.selectTCountById(req.getId());
        return R.ok().put("tCount", tCount);
    }


    /**
     * 更新盘点
     *
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/count/tCountEdit")
    public R tCountEdit(@RequestBody TCountReq req) throws Exception {
        TCount tCount = tCountService.selectTCountById(req.getId());
        BeanUtils.copyProperty(tCount, req, true);
        tCountService.updateTCountById(tCount);
        return R.ok("编辑成功");
    }

    /**
     * 删除盘点
     *
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/count/tCountDelete")
    @RequiresPermissions("user:device:count:delete")
    public R tCountDelete(@RequestBody TCountReq req) throws Exception {
        TCount tcount = tCountService.selectTCountById(req.getId());

        EquipmentInfo equipmentInfo = equipmentInfoService.selectEquipmentInfoByCount(tcount.getNum());
        if(equipmentInfo == null){
            tCountService.deleteTCountById(req.getId());
            return R.ok("删除成功");
        }
        return R.error("改次盘点下面有盘点设备，无法删除");

    }



    /**
     * 删除盘点
     *
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/count/newStart")
    @RequiresPermissions("user:device:count:save")
    public R newStart() throws Exception {
        TCount tCount = tCountService.selectMaxTCount();
        int count = 1 ;
        if(tCount !=null){
            count = tCount.getNum() + 1;
        }
        TCount newTcount = new TCount();
        newTcount.setNum(count);
        tCountService.insertTCount(newTcount);
        return R.ok("新轮盘点已经开始");
    }



    /**
     * 获取盘点集合
     *
     * @return 返回页面
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/count/tCountManager")
    public R tCountManager() throws Exception {
        return R.ok();
    }

    /**
     * 获取盘点集合
     *
     * @throws Exception 异常
     */
    @RequestMapping(value = "/modules/count/tCountList")
    @RequiresPermissions("user:device:count:list")
    public R tCountList(@RequestBody TCountReq req) throws Exception {
        PageUtils page = tCountService.selectByReq(req);
        return R.ok().put("total", 1)
                .put("rows", page);
    }
}
