package com.nantian.cloud.jly.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jly.entity.CheckPersons;
import com.nantian.cloud.jly.entity.CheckPersonsAmend;
import com.nantian.cloud.jly.entity.CompanyCalculater;
import com.nantian.cloud.jly.entity.InspectionData;
import com.nantian.cloud.jly.service.CheckPersonsAmendService;
import com.nantian.cloud.jly.service.CheckPersonsService;
import com.nantian.cloud.jly.service.CompanyCalculaterService;
import com.nantian.cloud.jly.service.InspectionDataService;
import com.nantian.cloud.jly.vo.JJF1356Vo;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import com.nantian.sc.cloud.common.minio.service.MinioTemplate;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.xmlbeans.impl.piccolo.io.FileFormatException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.*;


/**
 * 能源计量人员表
 *
 * @author
 * @date 2018-09-27 15:20:44
 */
@RestController
@AllArgsConstructor
@Api(value = "checkPersonsAmend", tags = "能源计量人员修改表")
@RequestMapping("/checkPersonsAmend")
public class CheckPersonsAmendController {
    private final CheckPersonsService checkPersonsService;
    private final CheckPersonsAmendService checkPersonsAmendService;

    /**
     * 查询计量人员，如果企业没有上传计量人员，就查询企业计量人员信息表
     *
     * @return
     */
    @SysLog("能源计量人员修改表-查询")
    @GetMapping("/queryCheckPersonsAmend")
    public R getCheckPersonsAmend(BigInteger checkPlanId, String epc) {
        QueryWrapper<CheckPersonsAmend> queryWrapperPro = new QueryWrapper<>();
        queryWrapperPro.eq("check_plan_id", checkPlanId);
        queryWrapperPro.eq("epc", epc);
        queryWrapperPro.eq("del_flag", "1");
        List<CheckPersonsAmend> checkPersonsAmendList = checkPersonsAmendService.list(queryWrapperPro);
        if (checkPersonsAmendList.size() > 0) {
            return R.ok(checkPersonsAmendList);
        } else {
            QueryWrapper<CheckPersons> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("check_plan_id", checkPlanId);
            queryWrapper.eq("epc", epc);
            queryWrapper.eq("del_flag", "1");
            List<CheckPersons> list = checkPersonsService.list(queryWrapper);
//            list.forEach(r->{
//                r.setId(null);
//            });
            return R.ok(list);
        }
    }


    /**
     * 新增
     */
    @SysLog("能源计量人员修改表-新增")
    @PostMapping("/saveCheckPersonsAmend")
    public R save(@RequestBody List<CheckPersonsAmend> checkPersonsAmend) {
        ScUser u = SecurityUtils.getUser();
        checkPersonsAmend.forEach(r -> {
            r.setUserid(u.getId());
            r.setDeptId(u.getDeptId());
            r.setDelFlag("1");
            r.setUpdateTime(new Date());
        });
        checkPersonsAmendService.deleteAmend(checkPersonsAmend.get(1).getCheckPlanId(),checkPersonsAmend.get(1).getEpc());
        boolean save = checkPersonsAmendService.saveOrUpdateBatch(checkPersonsAmend);
        return save ? R.ok("成功") : R.failed("失败");
    }


    /**
     * 导出word文件
     * @param listMap
     * @param response
     * @return
     * @throws IOException
     * @throws InvalidFormatException
     */
    @SysLog("能源计量人员修改表-getJJF1356")
    @PostMapping("/getJJF1356")
    public R getJJF1356(@RequestBody String listMap, HttpServletResponse response) throws IOException, InvalidFormatException {

        JSONObject json = JSONObject.parseObject(listMap);
        String string = json.getString("data");
        List<JJF1356Vo> jjf1356Vos = JSONArray.parseArray(string, JJF1356Vo.class);
        checkPersonsAmendService.getJJF1356(jjf1356Vos, response);
        return null;
    }

}
