package com.bonus.sgzb.material.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.bonus.sgzb.common.core.enums.ScrapSourceEnum;
import com.bonus.sgzb.material.domain.*;
import com.bonus.sgzb.material.service.IScrapApplyDetailsService;
import com.bonus.sgzb.material.vo.ForecastWasteExcel;
import com.bonus.sgzb.material.vo.ScrapApplyDetailsVO;
import com.bonus.sgzb.material.vo.ScrapAudit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
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.bonus.sgzb.common.log.annotation.Log;
import com.bonus.sgzb.common.log.enums.BusinessType;
import com.bonus.sgzb.common.security.annotation.RequiresPermissions;
import com.bonus.sgzb.common.core.web.controller.BaseController;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.common.core.utils.poi.ExcelUtil;
import com.bonus.sgzb.common.core.web.page.TableDataInfo;

/**
 * 报废任务详细
 * 
 * @author bonus
 * @date 2023-12-15
 */
@RestController
@Api(value = "报废任务详细")
@RequestMapping("/scrap")
public class ScrapApplyDetailsController extends BaseController
{
    @Resource
    private IScrapApplyDetailsService scrapApplyDetailsService;

    /**
     * 查询报废任务列表
     */
    @ApiOperation("查询报废任务列表")
    @GetMapping("/getScrapTaskList")
    public TableDataInfo list(ScrapApplyDetails scrapApplyDetails)
    {
        startPage();
        Map<String, Object> params = scrapApplyDetails.getParams();
        if (!params.isEmpty()){
            String beginTime = (String) params.get("beginTime");
            String endTime = (String) params.get("endTime");
            params.put("beginTime", beginTime+" 00:00:00");
            params.put("endTime", endTime+" 23:59:59");
            scrapApplyDetails.setParams(params);
        }
        List<ScrapApplyDetailsVO> list = scrapApplyDetailsService.selectScrapApplyDetailsList(scrapApplyDetails);
        return getDataTable(list);
    }

    /**
     * 导出报废任务列表
     */
    @Log(title = "导出报废任务列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScrapApplyDetails bean)
    {
        Map<String, Object> params = bean.getParams();
        if (params!=null && !params.isEmpty()){
            String beginTime = (String) params.get("beginTime");
            String endTime = (String) params.get("endTime");
            params.put("beginTime", beginTime+" 00:00:00");
            params.put("endTime", endTime+" 23:59:59");
            bean.setParams(params);
        }
        List<ScrapApplyDetailsVO> list = scrapApplyDetailsService.exportScrapApplyDetailsList(bean);
        ExcelUtil<ScrapApplyDetailsVO> util = new ExcelUtil<ScrapApplyDetailsVO>(ScrapApplyDetailsVO.class);
        util.exportExcel(response, list, "报废任务列表");
    }

    /**
     * 查看报废任务详细列表
     */
    @ApiOperation("查看报废任务详细列表")
    @GetMapping("/getScrapAuditList")
    public TableDataInfo getScrapAuditList(ScrapApplyDetails scrapApplyDetails)
    {
        startPage();
        List<ScrapApplyDetails> list = scrapApplyDetailsService.getScrapAuditList(scrapApplyDetails);
        return getDataTable(list);
    }

    /**
     * 报废任务审核
     */
    @ApiOperation("报废任务审核")
    @Log(title = "报废任务审核", businessType = BusinessType.UPDATE)
    @PostMapping("/audit")
    public AjaxResult audit(@RequestBody ScrapAudit scrapAudit) throws Exception {
        return toAjax(scrapApplyDetailsService.auditScrap(scrapAudit));
    }

    /**
     * 查看报废明细
     */
    @ApiOperation("查看报废明细")
    @GetMapping("/getScrapRecord")
    public TableDataInfo getScrapRecord(ScrapApplyDetails scrapApplyDetails)
    {
        startPage();
        List<RepairRecord> list = scrapApplyDetailsService.getScrapRecord(scrapApplyDetails);
        return getDataTable(list);
    }

    /**
     * 查看报废照片
     */
    @ApiOperation("查看报废照片")
    @GetMapping("/getFileUrl")
    public AjaxResult getFileUrl(String fileIds)
    {
        List<String> list = scrapApplyDetailsService.getFileUrl(fileIds);
        return AjaxResult.success(list);
    }

    /**
     * 查询预报废任务列表
     */
    @ApiOperation("查询预报废任务列表")
    @PostMapping("/getScrapApplyList")
    public TableDataInfo getScrapApplyList(ScrapApplyDetails scrapApplyDetails)
    {
        startPage();
        Map<String, Object> params = scrapApplyDetails.getParams();
        if (!params.isEmpty()){
            String beginTime = (String) params.get("beginTime");
            String endTime = (String) params.get("endTime");
            params.put("beginTime", beginTime+" 00:00:00");
            params.put("endTime", endTime+" 23:59:59");
            scrapApplyDetails.setParams(params);
        }
        List<ScrapApplyDetailsVO> list = scrapApplyDetailsService.getScrapApplyList(scrapApplyDetails);
        return getDataTable(list);
    }


    /**
     * 预报废任务审核
     */
    @ApiOperation("预报废任务审核")
    @Log(title = "预报废任务审核", businessType = BusinessType.UPDATE)
    @PostMapping("/forecastWasteAudit")
    public AjaxResult forecastWasteAudit(@RequestBody ScrapAudit scrapAudit) throws Exception {
        return toAjax(scrapApplyDetailsService.forecastWasteAudit(scrapAudit));
    }

    /**
     * 报废任务提交审计信息
     */
    @ApiOperation("报废任务提交审计信息")
    @Log(title = "报废任务提交审计信息", businessType = BusinessType.UPDATE)
    @PostMapping("/submitScrapTask")
    public AjaxResult submitScrapTask(@RequestBody ScrapAudit scrapAudit){
        return toAjax(scrapApplyDetailsService.submitScrapTask(scrapAudit));
    }

    /**
     * 查询报废审核任务列表
     */
    @ApiOperation("查询报废审核任务列表")
    @PostMapping("/scrapTaskReviewList")
    public TableDataInfo scrapTaskReviewList(ScrapApplyDetails scrapApplyDetails)
    {
        startPage();
        Map<String, Object> params = scrapApplyDetails.getParams();
        if (!params.isEmpty()){
            String beginTime = (String) params.get("beginTime");
            String endTime = (String) params.get("endTime");
            params.put("beginTime", beginTime+" 00:00:00");
            params.put("endTime", endTime+" 23:59:59");
            scrapApplyDetails.setParams(params);
        }
        List<ScrapApplyDetailsVO> list = scrapApplyDetailsService.scrapTaskReviewList(scrapApplyDetails);
        return getDataTable(list);
    }


    /**
     * 报废任务审核----宁夏
     */
    @ApiOperation("报废任务审核----宁夏")
    @Log(title = "报废任务审核----宁夏", businessType = BusinessType.UPDATE)
    @PostMapping("/scrapTaskReview")
    public AjaxResult scrapTaskReview(@RequestBody ScrapAuditorSet scrapAuditorSet) {
        return toAjax(scrapApplyDetailsService.scrapTaskReview(scrapAuditorSet));
    }


    /**
     * 获取处置列表----宁夏
     */
    @ApiOperation("获取处置列表----宁夏")
    @Log(title = "获取处置列表----宁夏", businessType = BusinessType.UPDATE)
    @PostMapping("/obtainDisposalList")
    public TableDataInfo obtainDisposalList(@RequestBody ScrapApplyDetails scrapApplyDetails) {
        startPage();
        Map<String, Object> params = scrapApplyDetails.getParams();
        if (!params.isEmpty()){
            String beginTime = (String) params.get("beginTime");
            String endTime = (String) params.get("endTime");
            params.put("beginTime", beginTime+" 00:00:00");
            params.put("endTime", endTime+" 23:59:59");
            scrapApplyDetails.setParams(params);
        }
        List<ScrapApplyDetailsVO> list = scrapApplyDetailsService.obtainDisposalList(scrapApplyDetails);
        return getDataTable(list);
    }


    /**
     * 处置----宁夏
     */
    @ApiOperation("处置----宁夏")
    @Log(title = "处置----宁夏", businessType = BusinessType.UPDATE)
    @PostMapping("/obtainDisposal")
    public AjaxResult obtainDisposal(@RequestBody List<TmTask> tmTaskList) {

        return toAjax(scrapApplyDetailsService.obtainDisposal(tmTaskList));
    }

    /**
     * 导出预报废审核列表
     */
    @Log(title = "导出预报废审核列表", businessType = BusinessType.EXPORT)
    @PostMapping("/exportForecastWaste")
    public void exportForecastWaste(HttpServletResponse response, ScrapApplyDetails scrapApplyDetails)
    {
        Map<String, Object> params = scrapApplyDetails.getParams();
        if (params!=null && !params.isEmpty()){
            String beginTime = (String) params.get("beginTime");
            String endTime = (String) params.get("endTime");
            params.put("beginTime", beginTime+" 00:00:00");
            params.put("endTime", endTime+" 23:59:59");
            scrapApplyDetails.setParams(params);
        }
        //获取预报废列表
        List<ForecastWasteExcel> ForecastWasteExcelList = scrapApplyDetailsService.exportForecastWaste(scrapApplyDetails);
        ExcelUtil<ForecastWasteExcel> util = new ExcelUtil<>(ForecastWasteExcel.class);
        util.exportExcel(response, ForecastWasteExcelList, "预报废审核列表");
    }

}
