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.auth.api.entity.SysUserRole;
import com.ym.mec.biz.dal.dao.EmployeeDao;
import com.ym.mec.biz.dal.entity.Employee;
import com.ym.mec.biz.dal.entity.InspectionItem;
import com.ym.mec.biz.dal.enums.SysUserRoleEnum;
import com.ym.mec.biz.dal.page.InspectionQueryInfo;
import com.ym.mec.biz.service.InspectionItemService;
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.entity.Mapper;
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.*;

@Api(tags = "巡查任务事项")
@RequestMapping("inspectionItem")
@RestController
public class InspectionItemController extends BaseController {
    @Autowired
    private InspectionItemService inspectionItemService;
    @Autowired
    private OrganizationService organizationService;
    @Resource
    private SysUserFeignService sysUserFeignService;

    @ApiOperation(value = "修改巡查任务事项")
    @PostMapping("/update")
    @PreAuthorize("@pcs.hasPermissions('inspectionItem/update')")
    public HttpResponseResult<InspectionItem> update(InspectionItem inspectionItem) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        inspectionItem.setUpdateTime(new Date());
        inspectionItemService.update(inspectionItem);
        return succeed(inspectionItem);
    }

    @ApiOperation(value = "巡查任务事项列表")
    @RequestMapping("/queryPage")
    @PreAuthorize("@pcs.hasPermissions('inspectionItem/queryPage')")
    public HttpResponseResult<PageInfo<InspectionItem>> queryPage(InspectionQueryInfo queryInfo) {
        queryInfo = organizationService.onlyEducation(queryInfo);
        String startTime = DateUtil.format(DateUtil.getFirstDayOfMonth(DateUtil.addMonths(new Date(), -2)),DateUtil.ISO_EXPANDED_DATE_FORMAT);
        queryInfo.setSearchStartTime(startTime);
        return succeed(inspectionItemService.getPageList(queryInfo));
    }
}
