package com.aucma.dms.controller;

import com.aucma.common.core.controller.BaseController;
import com.aucma.common.core.domain.AjaxResult;
import com.aucma.common.core.page.TableDataInfo;
import com.aucma.common.utils.poi.ExcelUtil;
import com.aucma.dms.domain.DmsBillsFaultInstance;
import com.aucma.dms.domain.DmsFaultCompentsParts;
import com.aucma.dms.domain.DmsFaultInstanceActivity;
import com.aucma.dms.domain.DmsKnowledgeRepair;
import com.aucma.dms.service.IDmsBillsFaultInstanceService;
import com.aucma.dms.service.IDmsKnowledgeRepairService;
import com.aucma.dms.utils.TemplateExcelUtils;
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.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;




/**
 * 检修工单Controller
 * 
 * @author Open Platform
 * @date 2024-01-23
 */
@RestController
@RequestMapping("/dms/dmsBillsFaultInstance")
public class DmsBillsFaultInstanceController extends BaseController
{
    @Autowired
    private IDmsBillsFaultInstanceService dmsBillsFaultInstanceService;
    
    @Autowired
    private IDmsKnowledgeRepairService dmsKnowledgeRepairService;

    /**
     * 查询维修知识库列表（用于下拉选择）
     */
    @GetMapping("/knowledge/list")
    public AjaxResult listKnowledge(DmsKnowledgeRepair dmsKnowledgeRepair)
    {
        List<DmsKnowledgeRepair> list = dmsKnowledgeRepairService.selectDmsKnowledgeRepairList(dmsKnowledgeRepair);
        return success(list);
    }

    /**
     * 查询检修工单列表
     */
    //@PreAuthorize("@ss.hasPermi('dms:dmsBillsFaultInstance:list')" )
    @GetMapping("/list")
    public TableDataInfo list(DmsBillsFaultInstance dmsBillsFaultInstance)
    {
        startPage();
        List<DmsBillsFaultInstance> list = dmsBillsFaultInstanceService.selectDmsBillsFaultInstanceList(dmsBillsFaultInstance);
        return getDataTable(list);
    }
    @GetMapping("/getDmsPartsList")
    public TableDataInfo getDmsPartsList(DmsBillsFaultInstance dmsBillsFaultInstance)
    {
        startPage();
        List<DmsFaultCompentsParts> list = dmsBillsFaultInstanceService.getDmsPartsList(dmsBillsFaultInstance.getRepairInstanceId());
        return getDataTable(list);
    }

    @GetMapping("/list/completed")
    public TableDataInfo completed(DmsBillsFaultInstance dmsBillsFaultInstance)
    {
        startPage();
        List<DmsBillsFaultInstance> list = dmsBillsFaultInstanceService.selectDmsBillsFaultInstanceCompletedList(dmsBillsFaultInstance);
        return getDataTable(list);
    }

    /**
     * 查询检修工单列表（不分页）
     */
    //@PreAuthorize("@ss.hasPermi('dms:dmsBillsFaultInstance:list')" )
    @GetMapping("/listAll")
    public AjaxResult listAll(DmsBillsFaultInstance dmsBillsFaultInstance)
    {
        List<DmsBillsFaultInstance> list = dmsBillsFaultInstanceService.selectDmsBillsFaultInstanceList(dmsBillsFaultInstance);
        return success(list);
    }

    /**
     * 导出检修工单列表
     */
    //@PreAuthorize("@ss.hasPermi('dms:dmsBillsFaultInstance:export')" )
    ////@Log(title = "检修工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DmsBillsFaultInstance dmsBillsFaultInstance)
    {
        List<DmsBillsFaultInstance> list = dmsBillsFaultInstanceService.selectDmsBillsFaultInstanceList(dmsBillsFaultInstance);
        ExcelUtil<DmsBillsFaultInstance> util = new ExcelUtil<DmsBillsFaultInstance>(DmsBillsFaultInstance.class);
        util.exportExcel(response, list, "检修工单数据");
    }

    /**
     * 获取检修工单详细信息
     */
    //@PreAuthorize("@ss.hasPermi('dms:dmsBillsFaultInstance:query')" )
    @GetMapping(value = "/{repairInstanceId}")
    public AjaxResult getInfo(@PathVariable("repairInstanceId") Long repairInstanceId)
    {
        return success(dmsBillsFaultInstanceService.selectDmsBillsFaultInstanceByRepairInstanceId(repairInstanceId));
    }

    /**
     * 新增检修工单
     */
    //@PreAuthorize("@ss.hasPermi('dms:dmsBillsFaultInstance:add')" )
    ////@Log(title = "检修工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DmsBillsFaultInstance dmsBillsFaultInstance)
    {
        return toAjax(dmsBillsFaultInstanceService.insertDmsBillsFaultInstance(dmsBillsFaultInstance));
    }

    /**
     * 修改检修工单
     */
    //@PreAuthorize("@ss.hasPermi('dms:dmsBillsFaultInstance:edit')" )
    ////@Log(title = "检修工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DmsBillsFaultInstance dmsBillsFaultInstance)
    {
        return toAjax(dmsBillsFaultInstanceService.updateDmsBillsFaultInstance(dmsBillsFaultInstance));
    }

    /**
     * 指定派工
     */
    //@PreAuthorize("@ss.hasPermi('dms:dmsBillsFaultInstance:edit')")
    @PostMapping("/dispatch")
    public AjaxResult dispatch(@RequestBody DmsFaultInstanceActivity dmsFaultInstanceActivity)
    {
        return toAjax(dmsBillsFaultInstanceService.dispatchRepair(dmsFaultInstanceActivity));
    }

    /**
     * 删除检修工单
     */
    //@PreAuthorize("@ss.hasPermi('dms:dmsBillsFaultInstance:remove')" )
    ////@Log(title = "检修工单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{repairInstanceIds}")
    public AjaxResult remove(@PathVariable Long[] repairInstanceIds)
    {
        return toAjax(dmsBillsFaultInstanceService.deleteDmsBillsFaultInstanceByRepairInstanceIds(repairInstanceIds));
    }
    @PostMapping("faultRecordExport")
    public void faultRecordExport(DmsBillsFaultInstance dmsBillsFaultInstance,HttpServletResponse response) throws Exception {
        Map<String, Object> beanParams = new HashMap<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        DmsFaultInstanceActivity faulsts = dmsBillsFaultInstanceService.selectFaults(dmsBillsFaultInstance.getRepairInstanceId());
        beanParams.put("applyUser",faulsts.getApplyUser());
        beanParams.put("applyTime",format.format(faulsts.getApplyTime()));
        beanParams.put("deviceName",faulsts.getDeviceName());
        beanParams.put("deviceLocation",faulsts.getDeviceLocation());
        beanParams.put("deviceCode",faulsts.getDeviceCode());

        if (faulsts.getRealBeginTime()!=null){
            beanParams.put("realEndTime",format.format(faulsts.getRealEndTime()));
        }
        if (faulsts.getRealEndTime()!=null){
            beanParams.put("realBeginTime",format.format(faulsts.getRealBeginTime()));
        }
        if (faulsts.getUpdateTime()!=null){
            beanParams.put("confirmTime",format.format(faulsts.getUpdateTime()));
        }
//        beanParams.put("realEndTime",format.format(faulsts.getRealEndTime()));
        beanParams.put("faultDescription",faulsts.getFaultDescription());
        beanParams.put("checkedFault",faulsts.getCheckedFault());
        beanParams.put("repairContent",faulsts.getRepairContent());
        beanParams.put("protectedMethod",faulsts.getProtectedMethod());
        beanParams.put("repairer",faulsts.getRepairer());
        String repairConfirm;
        if (faulsts.getConfirmer()!=null){
            repairConfirm = faulsts.getConfirmer();
        }else {
            repairConfirm = "未完成";
        }
        beanParams.put("repairConfirm",repairConfirm);
        beanParams.put("list",faulsts.getParts());

        TemplateExcelUtils.downLoadExcel("维修记录","",beanParams, response);
    }
}
