package com.ruoyi.project.sems.safecheck.controller;

import java.util.List;

import com.ruoyi.project.sems.safecheck.mapper.TSpecPersonweekcheckMapper;
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 com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.sems.safecheck.domain.TSpecPersonweekcheck;
import com.ruoyi.project.sems.safecheck.service.ITSpecPersonweekcheckService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 每周个人安全检查Controller
 *
 * @author Li
 * @date 2023-08-29
 */
@RestController
@RequestMapping("/safecheck/personweekcheck")
public class TSpecPersonweekcheckController extends BaseController
{
    @Autowired
    private ITSpecPersonweekcheckService tSpecPersonweekcheckService;
    @Resource
    private TSpecPersonweekcheckMapper tSpecPersonweekcheckMapper;
    /**
     * 查询每周个人安全检查列表
     */
    @PreAuthorize("@ss.hasPermi('safecheck:personweekcheck:list')")
    @GetMapping("/list")
    public TableDataInfo list(TSpecPersonweekcheck tSpecPersonweekcheck)
    {
        startPage();
        List<TSpecPersonweekcheck> list = tSpecPersonweekcheckService.selectTSpecPersonweekcheckList(tSpecPersonweekcheck);
        return getDataTable(list);
    }

    /**
     * 查询每周个人安全检查列表
     */
    @PreAuthorize("@ss.hasPermi('safecheck:personweekcheck:list')")
    @GetMapping("/myList")
    public TableDataInfo myList(TSpecPersonweekcheck tSpecPersonweekcheck)
    {
        long userId = getUserId();
        tSpecPersonweekcheck.setCheckerId(userId);
        startPage();
        List<TSpecPersonweekcheck> list = tSpecPersonweekcheckMapper.selectMyList(tSpecPersonweekcheck);
        return getDataTable(list);
    }

    /**
     * 查询每周个人安全检查列表
     */
    @GetMapping("/getWeekcheckByWeek")
    public AjaxResult getWeekcheckByWeek(TSpecPersonweekcheck tSpecPersonweekcheck)
    {
        List<TSpecPersonweekcheck> list = tSpecPersonweekcheckMapper.selectTSpecPersonweekcheckListByWeek(tSpecPersonweekcheck);
        TSpecPersonweekcheck res = new TSpecPersonweekcheck();
        String lastWeekrisk = "";
        String thisWeekRisk = "";
        String nextWeekWork = "";
        if (list.size() > 0) {
            for (TSpecPersonweekcheck t: list
                 ) {
                if (!"无".equals(t.getLastWeekRisk())) {
                    lastWeekrisk = lastWeekrisk + t.getCheckerName() + ":" + t.getLastWeekRisk()+ "\n";
                }
                if (!"无".equals(t.getThisWeekRisk())) {
                    thisWeekRisk = thisWeekRisk + t.getCheckerName() + ":" + t.getThisWeekRisk()+ "\n";
                }
                if (!"无".equals(t.getNextWeekWork())) {
                    nextWeekWork = nextWeekWork + t.getCheckerName() + ":" + t.getNextWeekWork()+ "\n";
                }
            }
        }
        res.setLastWeekRisk(lastWeekrisk);
        res.setThisWeekRisk(thisWeekRisk);
        res.setNextWeekWork(nextWeekWork);
        return AjaxResult.success(res);
    }

    /**
     * 导出每周个人安全检查列表
     */
    @PreAuthorize("@ss.hasPermi('safecheck:personweekcheck:export')")
    @Log(title = "每周个人安全检查", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TSpecPersonweekcheck tSpecPersonweekcheck)
    {
        List<TSpecPersonweekcheck> list = tSpecPersonweekcheckService.selectTSpecPersonweekcheckList(tSpecPersonweekcheck);
        ExcelUtil<TSpecPersonweekcheck> util = new ExcelUtil<TSpecPersonweekcheck>(TSpecPersonweekcheck.class);
        return util.exportExcel(list, "personweekcheck");
    }

    /**
     * 获取每周个人安全检查详细信息
     */
    @PreAuthorize("@ss.hasPermi('safecheck:personweekcheck:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tSpecPersonweekcheckService.selectTSpecPersonweekcheckById(id));
    }

    /**
     * 新增每周个人安全检查
     */
    @PreAuthorize("@ss.hasPermi('safecheck:personweekcheck:add')")
    @Log(title = "每周个人安全检查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TSpecPersonweekcheck tSpecPersonweekcheck)
    {
        long userId = getUserId();
        tSpecPersonweekcheck.setCheckerId(userId);
        tSpecPersonweekcheck.setCreaterCode(userId);
        return toAjax(tSpecPersonweekcheckService.insertTSpecPersonweekcheck(tSpecPersonweekcheck));
    }

    /**
     * 修改每周个人安全检查
     */
    @PreAuthorize("@ss.hasPermi('safecheck:personweekcheck:edit')")
    @Log(title = "每周个人安全检查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TSpecPersonweekcheck tSpecPersonweekcheck)
    {
        return toAjax(tSpecPersonweekcheckService.updateTSpecPersonweekcheck(tSpecPersonweekcheck));
    }

    /**
     * 修改每日安全检查
     */
    @PreAuthorize("@ss.hasPermi('safecheck:weekcheck:edit')")
    @Log(title = "每周个人安全检查确认", businessType = BusinessType.UPDATE)
    @GetMapping("batchConfirm/{ids}")
    public AjaxResult batchConfirm(@PathVariable Long[] ids)
    {
        return toAjax(tSpecPersonweekcheckMapper.batchConfirmByids(ids));
    }

    /**
     * 修改每日安全检查
     */
    @PreAuthorize("@ss.hasPermi('safecheck:weekcheck:edit')")
    @Log(title = "每周个人安全检查取消", businessType = BusinessType.UPDATE)
    @GetMapping("batchCancel/{ids}")
    public AjaxResult batchCancel(@PathVariable Long[] ids)
    {
        return toAjax(tSpecPersonweekcheckMapper.batchCancel(ids));
    }

    /**
     * 删除每周个人安全检查
     */
    @PreAuthorize("@ss.hasPermi('safecheck:personweekcheck:remove')")
    @Log(title = "每周个人安全检查", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tSpecPersonweekcheckService.deleteTSpecPersonweekcheckByIds(ids));
    }
}
