package com.ruoyi.project.production.controller;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.production.domain.TFivesChangeRecord;
import com.ruoyi.project.production.domain.TFivesRecordDetails;
import com.ruoyi.project.production.service.ITFivesChangeRecordService;
import com.ruoyi.project.production.service.ITFivesRecordDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 区域检查整改记录Controller
 *
 * @author ruoyi
 * @date 2023-03-30
 */
@RestController
@RequestMapping("/production/record")
public class TFivesChangeRecordController extends BaseController {
    @Autowired
    private ITFivesChangeRecordService tFivesChangeRecordService;
    @Autowired
    private ITFivesRecordDetailsService tFivesRecordDetailsService;

    /**
     * 查询区域检查整改记录列表
     */
    @PreAuthorize("@ss.hasPermi('production:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(TFivesChangeRecord tFivesChangeRecord) {
        startPage();
        List<TFivesChangeRecord> list = tFivesChangeRecordService.selectTFivesChangeRecordList(tFivesChangeRecord);
        return getDataTable(list);
    }

    /**
     * 导出区域检查整改记录列表
     */
    @PreAuthorize("@ss.hasPermi('production:record:export')")
    @Log(title = "区域检查整改记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TFivesChangeRecord tFivesChangeRecord) {
        List<TFivesChangeRecord> list = tFivesChangeRecordService.selectTFivesChangeRecordList(tFivesChangeRecord);
        ExcelUtil<TFivesChangeRecord> util = new ExcelUtil<TFivesChangeRecord>(TFivesChangeRecord.class);
        return util.exportExcel(list, "record");
    }

    /**
     * 获取区域检查整改记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('production:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tFivesChangeRecordService.selectTFivesChangeRecordById(id));
    }

    /**
     * 新增区域检查整改记录
     */
    @PreAuthorize("@ss.hasPermi('production:record:add')")
    @Log(title = "区域检查整改记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TFivesChangeRecord tFivesChangeRecord) {
        tFivesChangeRecord.setCreatedate(new Date());
        tFivesChangeRecord.setCreaterCode(String.valueOf(getUserId()));
        tFivesChangeRecord.setCheckerId(String.valueOf(getUserId()));
        tFivesChangeRecord.setCheckerName(getNickName());
        return toAjax(tFivesChangeRecordService.insertTFivesChangeRecord(tFivesChangeRecord));
    }

    /**
     * 修改区域检查整改记录
     */
    @PreAuthorize("@ss.hasPermi('production:record:edit')")
    @Log(title = "区域检查整改记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TFivesChangeRecord tFivesChangeRecord) {
        tFivesChangeRecord.setUpdatedate(new Date());
        tFivesChangeRecord.setUpdaterCode(getUserId().toString());
        return toAjax(tFivesChangeRecordService.updateTFivesChangeRecord(tFivesChangeRecord));
    }

    @PreAuthorize("@ss.hasPermi('production:record:edit')")
    @Log(title = "区域检查整改记录", businessType = BusinessType.UPDATE)
    @PutMapping("/change")
    public AjaxResult change(@RequestBody TFivesChangeRecord tFivesChangeRecord) {
        if (StringUtils.isNotEmpty(tFivesChangeRecord.getChangeFile())) {
            TFivesRecordDetails tFivesRecordDetails = new TFivesRecordDetails();
            tFivesRecordDetails.setRecordId(tFivesChangeRecord.getId());
            tFivesRecordDetails.setChangeContent(tFivesChangeRecord.getChangeContent());
            tFivesRecordDetails.setChangeDate(tFivesChangeRecord.getChangeDate());
            tFivesRecordDetails.setChangeResult(tFivesChangeRecord.getChangeResult());
            tFivesRecordDetails.setFileId(tFivesChangeRecord.getChangeFile());
            tFivesRecordDetails.setCreatedate(new Date());
            tFivesRecordDetails.setCreaterCode(getUserId().toString());
            tFivesRecordDetailsService.insertTFivesRecordDetails(tFivesRecordDetails);
            tFivesChangeRecord.setUpdatedate(new Date());
            tFivesChangeRecord.setUpdaterCode(getUserId().toString());
            return toAjax(tFivesChangeRecordService.updateTFivesChangeRecord(tFivesChangeRecord));
        } else {
            return AjaxResult.error("请上传整改后的图片！");
        }
    }

    @PreAuthorize("@ss.hasPermi('production:record:approve')")
    @Log(title = "区域检查整改记录", businessType = BusinessType.UPDATE)
    @PutMapping("/approve")
    public AjaxResult approve(@RequestBody TFivesChangeRecord tFivesChangeRecord) {
        TFivesRecordDetails tFivesRecordDetails = new TFivesRecordDetails();
        tFivesRecordDetails.setRecordId(tFivesChangeRecord.getId());
        for (TFivesRecordDetails details : tFivesRecordDetailsService.selectTFivesRecordDetailsList(tFivesRecordDetails)) {
            if (StringUtils.isEmpty(details.getChangeResult())) {
                details.setChangeResult(tFivesChangeRecord.getChangeResult());
                details.setUpdatedate(new Date());
                details.setUpdaterCode(getUserId().toString());
                tFivesRecordDetailsService.updateTFivesRecordDetails(details);
            }
        }
        tFivesChangeRecord.setChangeResult(tFivesChangeRecord.getChangeResult());
        tFivesChangeRecord.setId(tFivesChangeRecord.getId());
        tFivesChangeRecord.setUpdatedate(new Date());
        tFivesChangeRecord.setUpdaterCode(getUserId().toString());
        return toAjax(tFivesChangeRecordService.updateTFivesChangeRecord(tFivesChangeRecord));
    }

    /**
     * 删除区域检查整改记录
     */
    @PreAuthorize("@ss.hasPermi('production:record:remove')")
    @Log(title = "区域检查整改记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tFivesChangeRecordService.deleteTFivesChangeRecordByIds(ids));
    }
}
