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.EmployeeDao;
import com.ym.mec.biz.dal.dao.MusicGroupDao;
import com.ym.mec.biz.dal.entity.Employee;
import com.ym.mec.biz.dal.entity.Inspection;
import com.ym.mec.biz.dal.entity.InspectionItemPlan;
import com.ym.mec.biz.dal.entity.MusicGroup;
import com.ym.mec.biz.dal.page.InspectionItemPlanQueryInfo;
import com.ym.mec.biz.service.InspectionItemPlanService;
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 com.ym.mec.util.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
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.Arrays;
import java.util.Date;
import java.util.List;

@Api(tags = "巡查事项日程")
@RequestMapping("inspectionItemPlan")
@RestController
public class InspectionItemPlanController extends BaseController {
    @Autowired
    private InspectionItemPlanService inspectionItemPlanService;
    @Resource
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private MusicGroupDao musicGroupDao;

    @ApiOperation(value = "添加巡查任务事项日程")
    @PostMapping("/add")
    @PreAuthorize("@pcs.hasPermissions('inspectionItemPlan/add')")
    public HttpResponseResult<InspectionItemPlan> add(InspectionItemPlan inspectionItemPlan) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        inspectionItemPlan.setOperatorUserId(sysUser.getId());
        return succeed(inspectionItemPlanService.add(inspectionItemPlan));
    }

    @ApiOperation(value = "修改巡查任务事项日程")
    @PostMapping("/update")
    @PreAuthorize("@pcs.hasPermissions('inspectionItemPlan/update')")
    public HttpResponseResult<InspectionItemPlan> update(InspectionItemPlan inspectionItemPlan) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        inspectionItemPlan.setOperatorUserId(sysUser.getId());
        inspectionItemPlanService.updatePlan(inspectionItemPlan);
        return succeed(inspectionItemPlan);
    }

    @ApiOperation(value = "巡查任务事项日程列表")
    @RequestMapping("/queryPage")
    @PreAuthorize("@pcs.hasPermissions('inspectionItemPlan/queryPage')")
    public HttpResponseResult<PageInfo<InspectionItemPlan>> queryPage(InspectionItemPlanQueryInfo queryInfo) {
        queryInfo.setOrganId(organizationService.getEmployeeOrgan(queryInfo.getOrganId()));
        if (queryInfo.getStartTime() != null) {
            queryInfo.setStartTime(DateUtil.trunc(queryInfo.getStartTime()));
        }
        if (queryInfo.getEndTime() != null) {
            queryInfo.setEndTime(DateUtil.getLastTimeWithDay(queryInfo.getEndTime()));
        }
        String startTime = DateUtil.format(DateUtil.getFirstDayOfMonth(DateUtil.addMonths(new Date(), -2)), DateUtil.ISO_EXPANDED_DATE_FORMAT);
        queryInfo.setSearchStartTime(startTime);
        return succeed(inspectionItemPlanService.queryPage(queryInfo));
    }


    @ApiOperation(value = "修改处理信息")
    @PostMapping("/updateMemo")
    @PreAuthorize("@pcs.hasPermissions('inspectionItemPlan/updateMemo')")
    public HttpResponseResult<InspectionItemPlan> updateMemo(Long id, String memo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        InspectionItemPlan inspectionItemPlan = inspectionItemPlanService.get(id);
        inspectionItemPlan.setMemo(memo);
        inspectionItemPlan.setUpdateTime(new Date());
        inspectionItemPlan.setOperatorUserId(sysUser.getId());
        inspectionItemPlanService.update(inspectionItemPlan);
        return succeed(inspectionItemPlan);
    }


    @ApiOperation(value = "获取乐团主管的乐团")
    @GetMapping("/getMusicGroup")
    @PreAuthorize("@pcs.hasPermissions('inspectionItemPlan/getMusicGroup')")
    public HttpResponseResult<List<MusicGroup>> getMusicGroup(Integer organId,Integer userId, String search) {
        return succeed(musicGroupDao.getByEduTeacher(organId, userId, search));
    }

}
