package com.ruoyi.web.controller.workTecket;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.worktecket.dto.WorkTicketBasicDataDTO;
import com.ruoyi.worktecket.dto.res.WorkTicketBasicDataResultDTO;
import com.ruoyi.worktecket.dto.res.WorkTicketOperateRecordDTO;
import com.ruoyi.worktecket.service.IWorkTicketBasicDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 作业票Controller
 *
 * @author ruoyi
 * @date 2023-08-04
 */
@Api(tags = "作业票信息")
@RestController
@RequestMapping("/worktecket/data")
public class WorkTicketController extends BaseController {
    @Autowired
    private IWorkTicketBasicDataService workTicketBasicDataService;

    /**
     * 查询作业票基础信息列表
     */
    @ApiOperation("PC首页查询作业票列表")
    @GetMapping("/pcList")
    public TableDataInfo pcList(WorkTicketBasicDataDTO workTicketBasicData) {
//        PC作业票；列表回显：
//        申请部门名称：applicantDeptName
//        申请人姓名：applicantUserName
//        作业单位名称：workTicketCompanyName
//        区域名称：workTicketAreaName
//
//        查询条件，注意：作业单位有值时，作业单位类型为必选条件
//        作业单位类型(10承包商、20内部)：workTicketCompanyType
//        作业单位名称（模糊查询）：workTicketCompanyName
//
//
//
        String workTicketCompanyName = workTicketBasicData.getWorkTicketCompanyName();
        String workTicketCompanyType = workTicketBasicData.getWorkTicketCompanyType();
        if(StringUtils.isNotBlank(workTicketCompanyName) && StringUtils.isBlank(workTicketCompanyType)){
            throw new RuntimeException("查询作业单位前，请先设置作业单位类型！");
        }

        startPage();

        List<WorkTicketBasicDataResultDTO> list = workTicketBasicDataService.selectWorkTicketList(workTicketBasicData);
        return getDataTable(list);
    }

    /**
     * 查询作业票基础信息列表
     */
    @ApiOperation("查询作业票信息列表")
    //TODO 20230920PreAuthorize("@ss.hasPermi('worktecket:data:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkTicketBasicDataDTO workTicketBasicData) {
//        PC作业票；列表回显：
//        申请部门名称：applicantDeptName
//        申请人姓名：applicantUserName
//        作业单位名称：workTicketCompanyName
//        区域名称：workTicketAreaName
//
//        查询条件，注意：作业单位有值时，作业单位类型为必选条件
//        作业单位类型(10承包商、20内部)：workTicketCompanyType
//        作业单位名称（模糊查询）：workTicketCompanyName
//
//
//
        String workTicketCompanyName = workTicketBasicData.getWorkTicketCompanyName();
        String workTicketCompanyType = workTicketBasicData.getWorkTicketCompanyType();
        if(StringUtils.isNotBlank(workTicketCompanyName) && StringUtils.isBlank(workTicketCompanyType)){
            throw new RuntimeException("查询作业单位前，请先设置作业单位类型！");
        }


        startPage();

        List<WorkTicketBasicDataResultDTO> list = workTicketBasicDataService.selectWorkTicketBasicDataList(workTicketBasicData);
        return getDataTable(list);
    }


    /**
     * 导出作业票基础信息列表
     */
    @ApiOperation("导出作业票信息列表")
    //TODO 20230920PreAuthorize("@ss.hasPermi('worktecket:data:export')")
    @Log(title = "作业票基础信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkTicketBasicDataDTO workTicketBasicData) {
        List<WorkTicketBasicDataResultDTO> list = workTicketBasicDataService.selectWorkTicketBasicDataList(workTicketBasicData);
        ExcelUtil<WorkTicketBasicDataResultDTO> util = new ExcelUtil<WorkTicketBasicDataResultDTO>(WorkTicketBasicDataResultDTO.class);
        util.exportExcel(response, list, "作业票基础信息数据");
    }

    /**
     * 获取作业票基础信息详细信息
     */
    @ApiOperation("获取作业票信息详细信息")
    //TODO 20230920PreAuthorize("@ss.hasPermi('worktecket:data:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(workTicketBasicDataService.selectWorkTicketBasicDataById(id));
    }

    /**
     * 申请作业
     */
    @ApiOperation("申请作业")
    //TODO 20230920PreAuthorize("@ss.hasPermi('worktecket:data:add')")
    @Log(title = "作业票基础信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WorkTicketBasicDataDTO workTicketBasicData) {
        return workTicketBasicDataService.insertWorkTicketBasicData(workTicketBasicData, true);
    }

    /**
     * 修改作业票信息
     */
    @ApiOperation("修改作业票信息")
    //TODO 20230920PreAuthorize("@ss.hasPermi('worktecket:data:edit')")
    @Log(title = "作业票基础信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkTicketBasicDataDTO workTicketBasicData) {
        return workTicketBasicDataService.updateWorkTicketBasicData(workTicketBasicData, true);
    }

    /**
     * 删除作业票
     */
    @ApiOperation("删除作业票")
    //TODO 20230920PreAuthorize("@ss.hasPermi('worktecket:data:remove')")
    @Log(title = "作业票基础信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(workTicketBasicDataService.deleteWorkTicketBasicDataByIds(ids));
    }


    @ApiOperation("获取作业票操作记录列表")
    @GetMapping(value = "/operateRecord/{id}")
    public AjaxResult getWorkTicketOperateRecord(@PathVariable("id") String id) {
        return workTicketBasicDataService.getWorkTicketOperateRecord(id);
    }
}
