package com.nantian.cloud.jly.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jly.entity.*;
import com.nantian.cloud.jly.service.CheckGaugesTypeReportAmendService;
import com.nantian.cloud.jly.service.CheckGaugesTypeReportService;
import com.nantian.cloud.jly.service.InspectionDataService;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
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.xmlbeans.impl.piccolo.io.FileFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 能源计量器具配备情况统计汇总表B.8续修改表
 *
 * @date 2020-09-14 12:01:57
 */
@RestController
@AllArgsConstructor
@Api(value = "checkGaugesTypeReportAmend", tags = "能源计量器具配备情况统计汇总表B.8续修改表")
@RequestMapping("/checkGaugesTypeReportAmend")
public class CheckGaugesTypeReportAmendController {

    @Autowired
    private CheckGaugesTypeReportAmendService checkGaugesTypeReportAmendService;

    @Autowired
    private CheckGaugesTypeReportService checkGaugesTypeReportService;

    /**
     * 查询
     * @return
     */
    @SysLog("查询能源计量器具配备情况统计汇总表B.8续")
    @ApiOperation(value = "查询能源计量器具配备情况统计汇总表B.8续", notes = "条件查询能源计量器具配备情况统计汇总表B.8续")
    @GetMapping("/query")
    public R getCheckGaugesReportAmendService(BigInteger checkPlanId, String epc) {
        QueryWrapper<CheckGaugesTypeReportAmend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("check_plan_id", checkPlanId);
        queryWrapper.eq("epc", epc);
        queryWrapper.eq("del_flag", "1");
        List<CheckGaugesTypeReportAmend> list = checkGaugesTypeReportAmendService.list(queryWrapper);
        if (list.size() > 0) {
            return R.ok(list,"1");
        } else {
            CheckGaugesTypeReport checkGaugesTypeReport = new CheckGaugesTypeReport();
            checkGaugesTypeReport.setCheckPlanId(checkPlanId.longValue());
            checkGaugesTypeReport.setEpc(epc);
            List<CheckGaugesTypeReport> listPro = checkGaugesTypeReportService.getInfoList(checkGaugesTypeReport);
//            QueryWrapper<CheckGaugesTypeReport> queryWrapperPro = new QueryWrapper<>();
//            queryWrapperPro.eq("epc", epc);
//            queryWrapperPro.eq("check_plan_id", checkPlanId);
//            queryWrapperPro.eq("del_flag", "1");
//            List<CheckGaugesTypeReport> listPro = checkGaugesTypeReportService.list(queryWrapperPro);
            return R.ok(listPro,"0");
        }
    }


    /**
     * 信息
     *
     * @param checkGaugesTypeReportAmendId
     * @return R
     */
    @SysLog("通过ID查询能源计量器具配备情况统计汇总表B.8续")
    @ApiOperation(value = "通过ID查询能源计量器具配备情况统计汇总表B.8续", notes = "通过ID查询能源计量器具配备情况统计汇总表B.8续")
    @ApiImplicitParam(name = "checkGaugesTypeReportAmendId", value = "能源计量器具配备情况统计汇总表B.8续id", required = true, paramType = "path", dataType = "int")
    @GetMapping("/{checkGaugesTypeReportAmendId}")
    public R getById(@PathVariable("checkGaugesTypeReportAmendId") Integer checkGaugesTypeReportAmendId) {
        return R.ok(checkGaugesTypeReportAmendService.getById(checkGaugesTypeReportAmendId));
    }

    /**
     * 批量保存
     *
     * @param checkGaugesTypeReportAmendList
     * @return R
     */
    @SysLog("保存能源计量器具配备情况统计汇总表B.8续")
    @ApiOperation(value = "保存能源计量器具配备情况统计汇总表B.8续", notes = "新增能源计量器具配备情况统计汇总表B.8续")
    @ApiImplicitParam(name = "checkGaugesTypeReport", value = "能源计量器具配备情况统计汇总表B.8续", required = true, dataType = "json", paramType = "path")
    @PostMapping("/saveList")
    public R saveList(@RequestBody List<CheckGaugesTypeReportAmend> checkGaugesTypeReportAmendList) {
        ScUser u = SecurityUtils.getUser();
        //先删除再保存
        //获取list第一个对象里的参数
        CheckGaugesTypeReportAmend checkGaugesTypeReportAmend1 = checkGaugesTypeReportAmendList.stream().findFirst().orElse(null);
        checkGaugesTypeReportAmendService.deleteByCheckPlanIdAndEpc(checkGaugesTypeReportAmend1.getCheckPlanId(),checkGaugesTypeReportAmend1.getEpc());

        List<CheckGaugesTypeReportAmend> newList = new ArrayList<>();
        for (CheckGaugesTypeReportAmend checkGaugesTypeReportAmend : checkGaugesTypeReportAmendList) {
            if (u.getId() != null) {
                checkGaugesTypeReportAmend.setUserid(u.getId());
            }
            checkGaugesTypeReportAmend.setDelFlag("1");
            checkGaugesTypeReportAmend.setUpdateTime(new Date());
            checkGaugesTypeReportAmend.setDeptId(u.getDeptId());
            newList.add(checkGaugesTypeReportAmend);
        }
        return R.ok(checkGaugesTypeReportAmendService.saveBatch(newList));
    }


}
