package com.jd.lightapplication.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jd.lightapplication.common.lang.R;
import com.jd.lightapplication.model.InspectionTemplate;
import com.jd.lightapplication.model.InspectionTemplateProject;
import com.jd.lightapplication.model.dto.InspectionTemplateAddOrUpdateDto;
import com.jd.lightapplication.model.dto.InspectionTemplateQueryDto;
import com.jd.lightapplication.service.InspectionTemplateProjectService;
import com.jd.lightapplication.service.InspectionTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@Slf4j
@RestController()
@RequestMapping("/api/inspection/template")
@Api(tags = "巡检模板表")
public class InspectionTemplateController {
    @Resource
    private InspectionTemplateService inspectionTemplateService;
    @Resource
    private InspectionTemplateProjectService inspectionTemplateProjectService;

    @GetMapping("/list")
    @ApiOperation(value = "1、分页查询")
    public R queryPageBean(Page page, InspectionTemplateQueryDto inspectionTemplateQueryDto) {
        return R.success(inspectionTemplateService.page(page, inspectionTemplateQueryDto));
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation(value = "2、新增或者修改")
    public R saveOrUpdate(@RequestBody InspectionTemplateAddOrUpdateDto inspectionTemplateAddOrUpdateDto) {
        inspectionTemplateService.saveOrUpdate(inspectionTemplateAddOrUpdateDto);
        return R.success();
    }

    @PostMapping("/deleteById")
    @ApiOperation(value = "3、删除")
    @Transactional(rollbackFor = Exception.class)
    public R deleteById(long id) {
        //先删除所有的中间表数据
        LambdaQueryWrapper<InspectionTemplateProject> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(InspectionTemplateProject::getTemplateId, id);
        inspectionTemplateProjectService.remove(wrapper);
        //再删除主表数据
        inspectionTemplateService.removeById(id);
        return R.success();
    }

    @GetMapping("/getByType")
    @ApiOperation(value = "4、根据模板类型查询全部的模板")
    public R getByType(Long type) {
        LambdaQueryWrapper<InspectionTemplate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(InspectionTemplate::getType, type);
        wrapper.eq(InspectionTemplate::getIsEnable, 10);
        return R.success(inspectionTemplateService.list(wrapper));
    }

    @GetMapping("/getProjectByTemplateId")
    @ApiOperation(value = "5、根据模板id查询所有巡检项目")
    public R getProjectByTemplateId(Long templateId) {
        return R.success(inspectionTemplateService.getProjectByTemplateId(templateId));
    }

    @GetMapping("/getDailyInspectionProject")
    @ApiOperation(value = "6、查询当前启用的日常巡检模板的巡检项目")
    public R getDailyInspectionProject() {
        return R.success(inspectionTemplateService.getDailyInspectionProject());
    }

    @PostMapping("/updateIsEnableById")
    @ApiOperation(value = "6、根据模板id修改模板启用状态")
    public R updateIsEnableById(Long templateId, Integer isEnable) {
        return R.success(inspectionTemplateService.updateIsEnableById(templateId, isEnable));
    }

}