package com.bonus.material.repair.controller;

import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

import cn.hutool.core.convert.Convert;
import com.bonus.common.biz.annotation.StoreLog;
import com.bonus.common.biz.config.ListPagingUtil;
import com.bonus.common.core.utils.ServletUtils;
import com.bonus.common.log.enums.OperaType;
import com.bonus.material.basic.domain.report.LeaseOutInfo;
import com.bonus.material.basic.domain.report.PurChaseReportInfo;
import com.bonus.material.common.annotation.PreventRepeatSubmit;
import com.bonus.material.repair.domain.RepairPart;
import com.bonus.material.repair.domain.RepairRecord;
import com.bonus.material.repair.domain.vo.RepairHomeInfo;
import com.bonus.material.repair.domain.vo.ScrapApplyDetailsVO;
import com.bonus.material.repair.domain.vo.ScrapAudit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
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.common.log.annotation.SysLog;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.material.repair.domain.RepairAuditDetails;
import com.bonus.material.repair.service.IRepairAuditDetailsService;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.page.TableDataInfo;

import static com.bonus.common.biz.enums.TmTaskTypeEnum.TM_TASK_REPAIR_AUDIT;

/**
 * 修试审核详细Controller
 * 
 * @author syruan
 */
@Api(tags = "修试审核详细接口")
@RestController
@RequestMapping("/repair_audit_details")
public class RepairAuditDetailsController extends BaseController {

    @Resource
    private IRepairAuditDetailsService repairAuditDetailsService;


    /**
     * 查询修试审核任务列表
     */
    @ApiOperation("查询修试审核任务列表")
    @GetMapping("/questList")
    @SysLog(title = "查询修试审核任务列表", businessType = OperaType.QUERY, module = "机具系统->查询修试审核任务列表")
    //@RequiresPermissions("service:auditing:list")
    public AjaxResult questList(RepairAuditDetails repairAuditDetails) {
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<ScrapApplyDetailsVO> list = repairAuditDetailsService.selectRepairQuestList(repairAuditDetails);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }

    /**
     * 查询修试审核任务详情
     * @param repairAuditDetails
     * @return
     */
    @ApiOperation("查询修试审核任务详情")
    @GetMapping("/getDetailsList")
    public TableDataInfo getDetailsList(RepairAuditDetails repairAuditDetails) {
        startPage();
        List<RepairAuditDetails> list = repairAuditDetailsService.getDetailsList(repairAuditDetails);
        return getDataTable(list);
    }

    /**
     * 导出修试审核任务列表
     */
    @PostMapping("/export")
    @SysLog(title = "导出修试审核任务列表", businessType = OperaType.EXPORT, module = "机具系统->导出修试审核任务列表")
    public void exportAudit(HttpServletResponse response, RepairAuditDetails bean) {
        if (Objects.isNull(bean)) {
            bean = new RepairAuditDetails();
        }
        if (Objects.isNull(bean.getTaskType())) {
            bean.setTaskType(TM_TASK_REPAIR_AUDIT.getTaskTypeId());
        }
        repairAuditDetailsService.queryTimeCope(bean);
        List<ScrapApplyDetailsVO> list = repairAuditDetailsService.selectRepairQuestList(bean);
        ExcelUtil<ScrapApplyDetailsVO> util = new ExcelUtil<>(ScrapApplyDetailsVO.class);
        util.exportExcel(response, list, "修试审核任务列表");
    }

    /**
     * 查看修饰审核任务详细列表
     */
    @ApiOperation("查看修饰审核任务详细列表")
    @GetMapping("/getRepairAuditList")
    //@RequiresPermissions("service:auditing:list")
    public AjaxResult getRepairAuditList(RepairAuditDetails repairAuditDetails) {
        List<RepairAuditDetails> list = repairAuditDetailsService.getRepairAuditList(repairAuditDetails);
        return AjaxResult.success(list);
    }

    /**
     * 查看维修记录
     */
    @ApiOperation("查看维修记录")
    @GetMapping("/getRepairRecord")
    public TableDataInfo getRepairRecord(RepairAuditDetails repairAuditDetails) {
        startPage();
        List<RepairRecord> list = repairAuditDetailsService.getRepairRecord(repairAuditDetails);
        return getDataTable(list);
    }

    /**
     * 查看配件记录
     */
    @ApiOperation("查看配件记录")
    @GetMapping("/getPartRecord")
    public TableDataInfo getPartRecord(RepairAuditDetails repairAuditDetails) {
        startPage();
        List<RepairPart> list = repairAuditDetailsService.getPartRecord(repairAuditDetails);
        return getDataTable(list);
    }


    /**
     * 修饰任务审核
     */
    @ApiOperation("修饰任务审核")
    @StoreLog(title = "修饰审核入库",  module = "维修管理->修饰审核入库")
    @PostMapping("/audit")
    public AjaxResult audit(@RequestBody ScrapAudit scrapAudit) throws Exception {
        return toAjax(repairAuditDetailsService.auditRepair(scrapAudit));
    }



    //-----------------------------TOP--COPY--BUTTON--GENDER------------------------------


    /**
     * 查询修试审核详细列表
     */
    @ApiOperation(value = "查询修试审核详细列表")
    @RequiresPermissions("repair:details:list")
    @GetMapping("/list")
    public TableDataInfo list(RepairAuditDetails repairAuditDetails) {
        startPage();
        List<RepairAuditDetails> list = repairAuditDetailsService.selectRepairAuditDetailsList(repairAuditDetails);
        return getDataTable(list);
    }

    /**
     * 导出修试审核详细列表
     */
    @ApiOperation(value = "导出修试审核详细列表")
    @PreventRepeatSubmit
    @RequiresPermissions("repair:details:export")
    @SysLog(title = "修试审核详细", businessType = OperaType.EXPORT, logType = 1,module = "仓储管理->导出修试审核详细")
    @PostMapping("/exportEasyExcel")
    public void export(HttpServletResponse response, RepairAuditDetails repairAuditDetails) {
        List<RepairAuditDetails> list = repairAuditDetailsService.selectRepairAuditDetailsList(repairAuditDetails);
        ExcelUtil<RepairAuditDetails> util = new ExcelUtil<>(RepairAuditDetails.class);
        util.exportExcel(response, list, "修试审核详细数据");
    }

    /**
     * 获取修试审核详细详细信息
     */
    @ApiOperation(value = "获取修试审核详细详细信息")
    @RequiresPermissions("repair:details:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(repairAuditDetailsService.selectRepairAuditDetailsById(id));
    }

    /**
     * 新增修试审核详细
     */
    @ApiOperation(value = "新增修试审核详细")
    @PreventRepeatSubmit
    @RequiresPermissions("repair:details:add")
    @SysLog(title = "修试审核详细", businessType = OperaType.INSERT, logType = 1,module = "仓储管理->新增修试审核详细")
    @PostMapping
    public AjaxResult add(@RequestBody RepairAuditDetails repairAuditDetails) {
        try {
            return toAjax(repairAuditDetailsService.insertRepairAuditDetails(repairAuditDetails));
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 修改修试审核详细

    @ApiOperation(value = "修改修试审核详细")
    @PreventRepeatSubmit
    @RequiresPermissions("repair:details:edit")
    @SysLog(title = "修试审核详细", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->修改修试审核详细")
    @PutMapping
    public AjaxResult edit(@RequestBody RepairAuditDetails repairAuditDetails) {
        try {
            return toAjax(repairAuditDetailsService.updateRepairAuditDetails(repairAuditDetails));
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }*/

    @ApiOperation(value = "(外层)批量修改修试审核详细")
    @PreventRepeatSubmit
    //@RequiresPermissions("repair:details:edit")
    @SysLog(title = "批量修试审核详细", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->批量修改修试审核详细")
    @PostMapping("/outerAudit")
    public AjaxResult outerAudit(@RequestBody @NotNull List<RepairAuditDetails> repairAuditDetails) {
        return toAjax(repairAuditDetailsService.outerAudit(repairAuditDetails));
    }

    /**
     * 批量修改修试审核详细
     */
    @ApiOperation(value = "(内层)批量修改修试审核详细")
    @PreventRepeatSubmit
    //@RequiresPermissions("repair:details:edit")
    @SysLog(title = "批量修试审核详细", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->批量修改修试审核详细")
    @PostMapping("/innerAudit")
    public AjaxResult edit(@RequestBody @NotNull List<RepairAuditDetails> repairAuditDetails) {
        return toAjax(repairAuditDetailsService.updateRepairAuditDetailsBatch(repairAuditDetails));
    }

    /**
     * 删除修试审核详细
     */
    @ApiOperation(value = "删除修试审核详细")
    @PreventRepeatSubmit
    @RequiresPermissions("repair:details:remove")
    @SysLog(title = "修试审核详细", businessType = OperaType.DELETE, logType = 1,module = "仓储管理->删除修试审核详细")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(repairAuditDetailsService.deleteRepairAuditDetailsByIds(ids));
    }

    /**
     * 查询修饰一览表
     * @param dto
     * @return
     */
    @ApiOperation(value = "查询修饰一览表")
    @GetMapping("/getRepairHomeList")
    public AjaxResult getRepairHomeList(RepairHomeInfo dto) {
        try {
            dto.setIsExport(1);
            Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
            Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
            List<RepairHomeInfo> pageList = repairAuditDetailsService.getRepairHomeList(dto);
            return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, pageList));
        } catch (Exception e) {
            return error("查询修饰一览表失败, " + e.getMessage());
        }
    }

    /**
     * 查询修饰一览表总计
     * @param dto
     * @return
     */
    @ApiOperation(value = "查询修饰一览表总计")
    @GetMapping("/getRepairHomeTotalList")
    public AjaxResult getRepairHomeTotalList(RepairHomeInfo dto) {
        try {
            dto.setIsExport(0);
            List<RepairHomeInfo> pageList = repairAuditDetailsService.getRepairHomeTotalList(dto);
            return AjaxResult.success(pageList);
        } catch (Exception e) {
            return error("查询修饰一览表总计失败, " + e.getMessage());
        }
    }

    /**
     * 导出修饰一览表
     * @param response
     * @param dto
     */
    @ApiOperation(value = "查询修饰一览表")
    @PostMapping("/exportRepairHomeList")
    public void exportRepairHomeList(HttpServletResponse response, RepairHomeInfo dto)
    {
        dto.setIsExport(0);
        List<RepairHomeInfo> list = repairAuditDetailsService.getRepairHomeList(dto);
        ExcelUtil<RepairHomeInfo> util = new ExcelUtil<>(RepairHomeInfo.class);
        util.exportExcel(response, list, "修饰一览表");
    }
}
