package com.ruoyi.web.controller.ehs;

import java.util.List;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ehsMyMessage.domain.EhsMyMessage;
import com.ruoyi.ehsMyMessage.service.IEhsMyMessageService;
import com.ruoyi.ehsRisk.domain.EhsRisk;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ehsHiddenDanger.domain.EhsHiddenDanger;
import com.ruoyi.ehsHiddenDanger.service.IEhsHiddenDangerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 隐患信息Controller
 *
 * @author ruoyi
 * @date 2023-12-11
 */
@Api(tags="隐患信息管理")
@RestController
@RequestMapping("/ehsHiddenDanger/ehsHiddenDanger")
public class EhsHiddenDangerController extends BaseController
{
    @Autowired
    private IEhsHiddenDangerService ehsHiddenDangerService;
	@Autowired
	private IEhsMyMessageService ehsMyMessageService;
/**
 * 查询隐患信息列表
 */
@PreAuthorize("@ss.hasPermi('ehsHiddenDanger:ehsHiddenDanger:list')")
@GetMapping("/list")
	public TableDataInfo list(EhsHiddenDanger ehsHiddenDanger)
	{
		startPage();
		List<EhsHiddenDanger> list = ehsHiddenDangerService.selectEhsHiddenDangerList(ehsHiddenDanger);
		return getDataTable(list);
	}

	/**
	 * 导出隐患信息列表
	 */
	@PreAuthorize("@ss.hasPermi('ehsHiddenDanger:ehsHiddenDanger:export')")
	@Log(title = "隐患信息", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, EhsHiddenDanger ehsHiddenDanger)
	{
		List<EhsHiddenDanger> list = ehsHiddenDangerService.selectEhsHiddenDangerList(ehsHiddenDanger);
		ExcelUtil<EhsHiddenDanger> util = new ExcelUtil<EhsHiddenDanger>(EhsHiddenDanger.class);
		util.exportExcel(response, list, "隐患信息数据", "隐患信息数据");
	}

	/**
	 * 获取隐患信息详细信息
	 */
	@PreAuthorize("@ss.hasPermi('ehsHiddenDanger:ehsHiddenDanger:query')")
	@GetMapping(value = "/{hiddenDangerId}")
	public AjaxResult getInfo(@PathVariable("hiddenDangerId") Long hiddenDangerId)
	{
		EhsHiddenDanger ehsHiddenDanger = ehsHiddenDangerService.selectEhsHiddenDangerByHiddenDangerId(hiddenDangerId);
		if(StringUtils.isNotEmpty(ehsHiddenDanger.getHiddenDangerLabel()) ){
			String[] s = ehsHiddenDanger.getHiddenDangerLabel().split(",");
			ehsHiddenDanger.setHiddenDangerLabelArray(s);
		}
		return success(ehsHiddenDanger);
	}

    /**
     * 新增隐患信息
     */
    @ApiOperation("新增隐患信息")
    @PreAuthorize("@ss.hasPermi('ehsHiddenDanger:ehsHiddenDanger:add')")
    @Log(title = "隐患信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EhsHiddenDanger ehsHiddenDanger)
    {
        return toAjax(ehsHiddenDangerService.save(ehsHiddenDanger));
    }

    /**
     * 修改隐患信息
     */
    @ApiOperation("修改隐患信息")
    @PreAuthorize("@ss.hasPermi('ehsHiddenDanger:ehsHiddenDanger:edit')")
    @Log(title = "隐患信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EhsHiddenDanger ehsHiddenDanger)
    {
        return toAjax(ehsHiddenDangerService.updateById(ehsHiddenDanger));
    }

    /**
     * 删除隐患信息
     */
    @ApiOperation("删除隐患信息")
    @PreAuthorize("@ss.hasPermi('ehsHiddenDanger:ehsHiddenDanger:remove')")
    @Log(title = "隐患信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{hiddenDangerIds}")
    public AjaxResult remove(@PathVariable Long[] hiddenDangerIds)
    {
        return toAjax(ehsHiddenDangerService.removeByIds(Arrays.asList(hiddenDangerIds)));
    }

	/**
	 * 督办
	 */
	@ApiOperation("督办隐患信息")
	//@PreAuthorize("@ss.hasPermi('ehsHiddenDanger:ehsHiddenDanger:supervise')")
	@Log(title = "督办信息", businessType = BusinessType.UPDATE)
	@GetMapping("/supervise/{hiddenDangerIds}")
	public AjaxResult supervise(@PathVariable Long[] hiddenDangerIds)
	{
		return toAjax(ehsHiddenDangerService.superviseEhsHiddenDanger(hiddenDangerIds));
	}

	/**
	 * 协办
	 */
	@ApiOperation("协办隐患信息")
	@PreAuthorize("@ss.hasPermi('ehsHiddenDanger:ehsHiddenDanger:assist')")
	@Log(title = "协办信息", businessType = BusinessType.UPDATE)
	@GetMapping("/assist")
	public AjaxResult assist(  EhsHiddenDanger ehsHiddenDanger )
	{
		return toAjax(ehsHiddenDangerService.assistEhsHiddenDanger(ehsHiddenDanger));
	}

	@PostMapping("/importTemplate")
	public void importTemplate(HttpServletResponse response) throws Exception {
		ExcelUtil<EhsHiddenDanger> util = new ExcelUtil<EhsHiddenDanger>(EhsHiddenDanger.class);
		util.importComboTemplateExcel(response, "隐患信息数据");
	}
	/**
	 * 批量导入
	 */
	@PostMapping("/importData")
	public AjaxResult importHubManage(MultipartFile file, Boolean updateSupport) throws Exception {
		ExcelUtil<EhsHiddenDanger> util = new ExcelUtil<>(EhsHiddenDanger.class);
		List<EhsHiddenDanger> list = util.importExcel(file.getInputStream());
		String message = ehsHiddenDangerService.importHiddenDanger(list, updateSupport);
		return AjaxResult.success(message);
	}

	/**
	 *
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/selectEhsHiddenDangerOverTime")
	public AjaxResult selectEhsHiddenDangerOverTime(MultipartFile file, Boolean updateSupport) throws Exception {
		ExcelUtil<EhsHiddenDanger> util = new ExcelUtil<>(EhsHiddenDanger.class);
		List<EhsHiddenDanger> list = util.importExcel(file.getInputStream());
		String message = ehsHiddenDangerService.importHiddenDanger(list, updateSupport);
		return AjaxResult.success(message);
	}
	@GetMapping("/getOverTimeId")
	public AjaxResult getOverTimeId()
	{
		//获取临期或超时的隐患id，比较本人的最后一条，如果不一样就插入一条，如果一样就不插入，新插入的是未读
		//如果点检首页弹窗的连接，就设置为已读，并转跳的隐患页面，并查询出所有临期或超时的隐患
		EhsHiddenDanger hdQuery = new EhsHiddenDanger();
		hdQuery.setDeptId(SecurityUtils.getDeptId());
		hdQuery.setOverTime(10);
		EhsHiddenDanger ehsHiddenDanger = ehsHiddenDangerService.selectOverTimeId(hdQuery);
		EhsMyMessage ehsMyMessage = new EhsMyMessage();
		ehsMyMessage.setMyMessageType("0");
		ehsMyMessage.setMyUserId(SecurityUtils.getUserId());
		EhsMyMessage msg = ehsMyMessageService.selectEhsMyMessageByMyMessageMax(ehsMyMessage);
		boolean isNew =false;
		if(ehsHiddenDanger == null){
			ehsMyMessage.setMyMessageDesc("");
			return success(ehsMyMessage);
		}
		if(msg==null   ){
			if(ehsHiddenDanger!=null && StringUtils.isNotEmpty(ehsHiddenDanger.getOverTimeId()))
				isNew = true;
			else
				isNew = false;
		}else{
			if(StringUtils.isNotEmpty(ehsHiddenDanger.getOverTimeId())) {
				if(!ehsHiddenDanger.getOverTimeId().equals(msg.getMyMessageKey())) {//两者不一致
					String[] newIds = ehsHiddenDanger.getOverTimeId().split(",");
					String oldIds = "," + msg.getMyMessageKey() + ",";
					for (String id : newIds) {
						if (oldIds.indexOf("," + id + ",") == -1) {//不包含，表示有新的id
							isNew = true;
							break;
						}
					}
				}else{//两者一致
					msg.setMyMessageDesc("");
					return success(msg);
				}
			}
		}
		if (isNew) {//有新的id，生成一个新记录
			EhsMyMessage msgNew = new EhsMyMessage();
			msgNew.setMyUserId(SecurityUtils.getUserId());
			msgNew.setMyMessageType("0");
			msgNew.setMyMessageDesc("您有新的将要逾期或已逾期的记录！");
			msgNew.setMyMessageKey(ehsHiddenDanger.getOverTimeId());
			msgNew.setMyMessageState("N");
			ehsMyMessageService.save(msgNew);
			return success(msgNew);
		}
		ehsMyMessage.setMyMessageDesc("");
		return success(ehsMyMessage);
	}
}
