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


/**
 * 年度重点用能单位能源购进、消费与库存情况修改表
 *
 * @author
 * @date 2018-09-27 15:20:44
 */
@RestController
@AllArgsConstructor
@RequestMapping("/checkStoreAmend")
@Api(value = "checkStoreAmend", tags = "年度重点用能单位能源购进、消费与库存情况修改表")
public class CheckStoreAmendController {
    private final CheckStoreAmendService checkStoreAmendService;
    private final CheckStoreService checkStoreService;


    /**
     * 查询
     *
     * @return
     */
    @SysLog("年度重点用能单位能源购进、消费与库存情况修改表-查询")
    @ApiOperation(value = "查询年度重点用能单位能源购进、消费与库存情况表", notes = "条件查询年度重点用能单位能源购进、消费与库存情况表")
    @GetMapping("/query")
    public R getCheckStoreAmend(BigInteger checkPlanId, String epc) {
        QueryWrapper<CheckStoreAmend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("check_plan_id", checkPlanId);
        queryWrapper.eq("epc", epc);
        queryWrapper.eq("del_flag", "1");
        List<CheckStoreAmend> storeAmendList = checkStoreAmendService.list(queryWrapper);
        if (storeAmendList.size() > 0) {
            return R.ok(storeAmendList,"保存数据");
        } else {
            QueryWrapper<CheckStore> queryWrapperPro = new QueryWrapper<>();
            queryWrapperPro.eq("epc", epc);
            queryWrapperPro.eq("check_plan_id", checkPlanId);
            queryWrapperPro.eq("del_flag", "1");
            List<CheckStore> listPro = checkStoreService.list(queryWrapperPro);
            return R.ok(listPro,"原始数据");
        }
    }


    /**
     * 信息
     *
     * @param checkStoreId
     * @return R
     */
    @SysLog("年度重点用能单位能源购进、消费与库存情况修改表-查询")
    @ApiOperation(value = "查询年度重点用能单位能源购进、消费与库存情况表", notes = "条件查询年度重点用能单位能源购进、消费与库存情况表")
    @ApiImplicitParam(name = "checkStoreId", value = "年度重点用能单位能源购进、消费与库存情况表id", required = true, dataType = "int", paramType = "path")
    @GetMapping("/{checkStoreId}")
    public R getById(@PathVariable("checkStoreId") Integer checkStoreId) {
        return R.ok(checkStoreAmendService.getById(checkStoreId));
    }

    /**
     * 保存
     *
     * @param checkStoreAmendList
     * @return R
     */
    @SysLog("年度重点用能单位能源购进、消费与库存情况修改表-保存")
    @ApiOperation(value = "保存年度重点用能单位能源购进、消费与库存情况表", notes = "新增年度重点用能单位能源购进、消费与库存情况表")
    @ApiImplicitParam(name = "CheckStoreAmend", value = "年度重点用能单位能源购进、消费与库存情况表", required = true, dataType = "JSON", paramType = "path")
    @PostMapping
    public R save(@RequestBody List<CheckStoreAmend> checkStoreAmendList) {
        ScUser u = SecurityUtils.getUser();
        //先删除再保存
        CheckStoreAmend checkStoreAmend1 = checkStoreAmendList.stream().findFirst().orElse(null);

        checkStoreAmendService.deleteByCheckPlanIdAndEpc(checkStoreAmend1.getCheckPlanId(),checkStoreAmend1.getEpc());

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


}
