package com.ym.mec.web.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.MusicGroupDao;
import com.ym.mec.biz.dal.entity.Inspection;
import com.ym.mec.biz.dal.page.InspectionQueryInfo;
import com.ym.mec.biz.service.InspectionService;
import com.ym.mec.biz.service.OrganizationService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.page.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "巡查任务")
@RequestMapping("inspection")
@RestController
public class InspectionController extends BaseController {
    @Autowired
    private InspectionService inspectionService;
    @Autowired
    private OrganizationService organizationService;
    @Resource
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private MusicGroupDao musicGroupDao;

    @ApiOperation(value = "添加巡查任务")
    @PostMapping("/add")
    @PreAuthorize("@pcs.hasPermissions('inspection/add')")
    public HttpResponseResult<List<Inspection>> add(@RequestBody List<Inspection> inspections) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        return succeed(inspectionService.add(inspections, sysUser.getId()));
    }

    @ApiOperation(value = "删除巡查任务")
    @PostMapping("/delete")
    @PreAuthorize("@pcs.hasPermissions('inspection/delete')")
    public HttpResponseResult<Boolean> delete(Long id) {
        return succeed(inspectionService.delInspections(id));
    }

    @ApiOperation(value = "修改巡查任务")
    @PostMapping("/update")
    @PreAuthorize("@pcs.hasPermissions('inspection/update')")
    public HttpResponseResult<Inspection> update(@RequestBody Inspection inspection) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        return succeed(inspectionService.updateInspection(inspection, sysUser.getId()));
    }

    @ApiOperation(value = "巡查任务列表")
    @GetMapping("/queryPage")
    @PreAuthorize("@pcs.hasPermissions('inspection/queryPage')")
    public HttpResponseResult<PageInfo<Inspection>> queryPage(InspectionQueryInfo queryInfo) {
        queryInfo.setOrganId(organizationService.getEmployeeOrgan(queryInfo.getOrganId()));
        return succeed(inspectionService.getPageList(queryInfo));
    }

    @ApiOperation(value = "查看巡查任务详情")
    @GetMapping("/getInfo")
    @PreAuthorize("@pcs.hasPermissions('inspection/getInfo')")
    public HttpResponseResult<Inspection> getInfo(Long id) {
        return succeed(inspectionService.getInfo(id));
    }


    @ApiOperation(value = "获取分部进行中乐团的乐团主管")
    @GetMapping("/getMusicGroupEduTeacher")
    @PreAuthorize("@pcs.hasPermissions('inspection/getMusicGroupEduTeacher')")
    public HttpResponseResult<List<SysUser>> getMusicGroupEduTeacher(Integer organId) {
        return succeed(musicGroupDao.getMusicGroupEduTeacher(organId));
    }

}
