package com.ruoyi.dimp.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.PreImpStatus;
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.poi.ExcelUtil;
import com.ruoyi.dimp.domain.DPctApplyInfoExp;
import com.ruoyi.dimp.domain.DPctApplyInfoPreDetail;
import com.ruoyi.dimp.domain.DPctApplyInfoPreImp;
import com.ruoyi.dimp.service.IDPctApplyInfoPreDetailService;
import com.ruoyi.dimp.service.IDPctApplyInfoPreImpService;

/**
 * PCT申请导入记录Controller
 *
 * @author ruoyi
 * @date 2023-03-19
 */
@RestController
@RequestMapping("/dimp/dPctApplyInfoPreImp")
public class DPctApplyInfoPreImpController extends BaseController {
    private final IDPctApplyInfoPreImpService dPctApplyInfoPreImpService;

    @Autowired
    private IDPctApplyInfoPreDetailService dPctApplyInfoPreDetailService;
    
    public DPctApplyInfoPreImpController(IDPctApplyInfoPreImpService dPctApplyInfoPreImpService) {
        this.dPctApplyInfoPreImpService = dPctApplyInfoPreImpService;
    }
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
		ExcelUtil<DPctApplyInfoExp> util = new ExcelUtil<DPctApplyInfoExp>(DPctApplyInfoExp.class);
		List<DPctApplyInfoExp>list=new ArrayList<DPctApplyInfoExp>();
		util.exportExcel(response, list, "PCT申请数据");
	}
    
    @PostMapping("/import")
	@Log(title = "PCT申请数据导入", businessType = BusinessType.IMPORT)
	@ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception {
    	DPctApplyInfoPreImp imp = dPctApplyInfoPreImpService.importData(file);
		// 正在解析
    	DPctApplyInfoPreImp u1 = new DPctApplyInfoPreImp();
		u1.setId(imp.getId());
		u1.setImpState(PreImpStatus.IMP_ING);
		dPctApplyInfoPreImpService.updateById(u1);
		Thread pdImpThread = new Thread() {
			public void run() {
				try {
					// 预处理
					List<DPctApplyInfoPreDetail> list = dPctApplyInfoPreDetailService.doPreImp(imp);
					// 处理
					dPctApplyInfoPreDetailService.doImp(list, imp);
				} catch (Exception e) {
					logger.error("导入时发生成异常," + e.getMessage(), e);

					DPctApplyInfoPreImp u1 = new DPctApplyInfoPreImp();
					u1.setId(imp.getId());
					u1.setImpState(PreImpStatus.PARSE_ERR);
					String errReason = e.getMessage();
					if (errReason.length() > 3000) {
						errReason = errReason.substring(0, 3000);
					}
					u1.setErrReason(errReason);
					dPctApplyInfoPreImpService.updateById(u1);

				}
			}
		};
		pdImpThread.setName("pdImpThread");
		pdImpThread.setDaemon(true);
		pdImpThread.start();
		return AjaxResult.success("系统已接收，请稍后留意导入状态");
    }


    

    /**
     * 查询PCT申请导入记录列表
     *
     * @param dPctApplyInfoPreImp PCT申请导入记录
     * @return PCT申请导入记录集合
     * @date 2023-03-19
     */
    @PreAuthorize("@ss.hasPermi('dimp:dPctApplyInfoPreImp:list')")
    @GetMapping("/list")
    public TableDataInfo list(DPctApplyInfoPreImp dPctApplyInfoPreImp) {
        startPage();
        List<DPctApplyInfoPreImp> list = dPctApplyInfoPreImpService.queryList(dPctApplyInfoPreImp);
        return getDataTable(list);
    }


    /**
     * 导出PCT申请导入记录列表
     *
     * @param dPctApplyInfoPreImp PCT申请导入记录
     * @date 2023-03-19
     */
    @PreAuthorize("@ss.hasPermi('dimp:dPctApplyInfoPreImp:export')")
    @Log(title = "PCT申请导入记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DPctApplyInfoPreImp dPctApplyInfoPreImp) {
        List<DPctApplyInfoPreImp> list = dPctApplyInfoPreImpService.list(new QueryWrapper<>());
        ExcelUtil<DPctApplyInfoPreImp> util = new ExcelUtil<>(DPctApplyInfoPreImp. class);
        util.exportExcel(response, list, "PCT申请导入记录数据");
    }

    /**
     * 获取PCT申请导入记录详细信息
     *
     * @param id 主键id
     * @return 结果
     * @date 2023-03-19
     */
    @PreAuthorize("@ss.hasPermi('dimp:dPctApplyInfoPreImp:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(dPctApplyInfoPreImpService.getById(id));
    }


    /**
     * 新增PCT申请导入记录信息
     *
     * @param dPctApplyInfoPreImp PCT申请导入记录
     * @return 结果
     * @date 2023-03-19
     */
    @PreAuthorize("@ss.hasPermi('dimp:dPctApplyInfoPreImp:add')")
    @Log(title = "PCT申请导入记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DPctApplyInfoPreImp dPctApplyInfoPreImp) {
        return toAjax(dPctApplyInfoPreImpService.save(dPctApplyInfoPreImp));
    }


    /**
     * 修改PCT申请导入记录信息
     *
     * @param dPctApplyInfoPreImp PCT申请导入记录
     * @return 结果
     * @date 2023-03-19
     */
    @PreAuthorize("@ss.hasPermi('dimp:dPctApplyInfoPreImp:edit')")
    @Log(title = "PCT申请导入记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DPctApplyInfoPreImp dPctApplyInfoPreImp) {
        return toAjax(dPctApplyInfoPreImpService.updateById(dPctApplyInfoPreImp));
    }


    /**
     * 删除PCT申请导入记录信息
     *
     * @param ids 主键ids
     * @return 结果
     * @date 2023-03-19
     */
    @PreAuthorize("@ss.hasPermi('dimp:dPctApplyInfoPreImp:remove')")
    @Log(title = "PCT申请导入记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(dPctApplyInfoPreImpService.removeByIds(Arrays.asList(ids)));
    }


}
