package com.yuchen.saas.park.service.fault.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.park.api.entity.fault.EquipmentBreakdownType;
import com.yuchen.saas.park.service.fault.service.EquipmentBreakdownTypeService;
import io.swagger.annotations.Api;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.tool.api.R;
import org.springframework.web.bind.annotation.PostMapping;
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: 肖润飞
 * @CreateTime: 2022/10/19 18:32
 * @Description: 故障类型
 * @Version: 1.0
 */
@RestController
@RequestMapping("/equipment_breakdown/type")
@Api(tags = "设备故障分类", value = "设备故障分类")
public class EquipmentBreakdownTypeController {


    @Resource
    private EquipmentBreakdownTypeService breakdownTypeService;

    @RequestMapping("/list_vo")
    public R listVo() {
        R result = breakdownTypeService.listVo();
        return result;
    }

    @RequestMapping("/list")
    public R list() {
        List<EquipmentBreakdownType> list = breakdownTypeService.list();
        return R.data(list);
    }

    @RequestMapping("/page")
    public R page(Query query, EquipmentBreakdownType type) {
        Page page = (Page) Condition.getPage(query);
        Page<EquipmentBreakdownType> res = breakdownTypeService.page(page, Condition.getQueryWrapper(type));
        R result = breakdownTypeService.pageVo(page, type);
        return result;
    }

    @PostMapping("/add")
    public R add(@RequestBody EquipmentBreakdownType type) {
        if (StrUtil.isBlank(type.getBreakdownCode())) {
            R.error("故障分类code不能为空");
        }
        LambdaQueryWrapper<EquipmentBreakdownType> query = Wrappers.lambdaQuery();
        query.eq(EquipmentBreakdownType::getBreakdownCode, type.getBreakdownCode());
        int count = breakdownTypeService.count(query);
        if (count > 0) {
            R.error("存在相同的breakdownCode故障标签");
        }
        breakdownTypeService.save(type);
        return R.data(true);
    }

    @PostMapping("/update")
    public R update(@RequestBody EquipmentBreakdownType type) {

        //breakdownTypeService.updateById(type);
        R result = breakdownTypeService.updateType(type);
        return result;
    }

    @PostMapping("/delete")
    public R delete(@RequestBody EquipmentBreakdownType type) {
        R result = breakdownTypeService.deleteType(type);
        return result;
    }

    @PostMapping("/update_batch")
    public R updateBatch(@RequestBody List<EquipmentBreakdownType> type) {
        breakdownTypeService.updateBatchById(type);
        return R.data(true);
    }

    @PostMapping("/is_show")
    public R isShow(@RequestBody List<EquipmentBreakdownType> types) {
        return breakdownTypeService.isShow(types);
    }

}