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.CheckLevelDegreeReportAmendService;
import com.nantian.cloud.jly.service.CheckLevelDegreeReportService;
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;

@RestController
@AllArgsConstructor
@Api(value = "checkLevelDegreeReportAmend",tags = "能源计量器具准确度等级统计汇总表B9")
@RequestMapping("/checkLevelDegreeReportAmend")
public class CheckLevelDegreeReportAmendController {
    @Autowired
    private CheckLevelDegreeReportService checkLevelDegreeReportService;

    @Autowired
    private CheckLevelDegreeReportAmendService checkLevelDegreeReportAmendService;


    /**
     * 分页查询
     *
     * @return
     */
    @SysLog("能源计量器具准确度等级统计汇总表B9-分页查询")
    @ApiOperation(value = "查询能源计量器具准确度等级统计汇总表B9",notes = "条件查询能源计量器具准确度等级统计汇总表B9")
    @GetMapping("/query")
    public R getCheckGaugesReportAmend(BigInteger checkPlanId,String epc) {
        QueryWrapper<CheckLevelDegreeReportAmend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("check_plan_id", checkPlanId);
        queryWrapper.eq("epc", epc);
        queryWrapper.eq("del_flag", "1");
        List<CheckLevelDegreeReportAmend> list = checkLevelDegreeReportAmendService.list(queryWrapper);
        if (list.size() > 0) {
            return R.ok(list);
        } else {
            QueryWrapper<CheckLevelDegreeReport> queryWrapperPro = new QueryWrapper<>();
            queryWrapperPro.eq("epc", epc);
            queryWrapperPro.eq("check_plan_id", checkPlanId);
            queryWrapperPro.eq("del_flag", "1");
            List<CheckLevelDegreeReport> listPro = checkLevelDegreeReportService.list(queryWrapperPro);
            return R.ok(listPro);
        }
    }

    /**
     * 批量保存
     *
     * @param checkLevelDegreeReportAmendList
     * @return R
     */
    @SysLog("能源计量器具准确度等级统计汇总表B9-批量保存")
    @ApiOperation(value = "保存能源计量器具准确度等级统计汇总表B9",notes = "新增能源计量器具准确度等级统计汇总表B9")
    @ApiImplicitParam(name ="CheckGauges",value = "能源计量器具准确度等级统计汇总表B9",required = true,dataType = "json",paramType = "path")
    @PostMapping("/saveList")
    public R saveList(@RequestBody List<CheckLevelDegreeReportAmend> checkLevelDegreeReportAmendList) {
        ScUser u= SecurityUtils.getUser();
        //先删除 再保存
        CheckLevelDegreeReportAmend checkLevelDegreeReportAmend1 = checkLevelDegreeReportAmendList.stream().findFirst().orElse(null);
        checkLevelDegreeReportAmendService.deleteByCheckPlanIdAndEpc(checkLevelDegreeReportAmend1.getCheckPlanId(),checkLevelDegreeReportAmend1.getEpc());
        //保存
        List<CheckLevelDegreeReportAmend> newList = new ArrayList<>();
        for (CheckLevelDegreeReportAmend checkLevelDegreeReportAmend : checkLevelDegreeReportAmendList) {
            if(u.getId()!=null){
                checkLevelDegreeReportAmend.setUserid(u.getId());
            }
            checkLevelDegreeReportAmend.setDelFlag("1");
            checkLevelDegreeReportAmend.setUpdateTime(new Date());
            checkLevelDegreeReportAmend.setDeptId(u.getDeptId());
            newList.add(checkLevelDegreeReportAmend);
        }
        return R.ok(checkLevelDegreeReportAmendService.saveBatch(newList));
    }



}
