package com.nantian.cloud.jljc.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.dto.AuditorDto;
import com.nantian.cloud.jljc.entity.*;
import com.nantian.cloud.jljc.service.*;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.DecimalFormat;
import java.util.*;


/**
 * 企业综合能源消费量限额
 *
 * @since 2021-01-17
 */
@Slf4j
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/tTce")
@Api(value = "tTce", tags = "企业综合能源消费量限额")
public class TTceController {

    final private TTceService tTceService;
    final private EnterpriseInfoService enterpriseInfoService;

    /**
     * 分页查询企业综合能源消费量限额
     */
    @SysLog("分页查询企业综合能源消费量限额")
    @GetMapping("/page")
    @ApiOperation(value = "分页查询企业综合能源消费量限额", notes = "分页查询企业综合能源消费量限额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "TTce", value = "企业综合能源消费量限额", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "page", value = "分页对象", required = false, dataType = "json", paramType = "path")
    })
    public R getTTcePage(Page page, TTce tTce) {
        return R.ok(tTceService.getTTcePage(page, tTce));
    }

    /**
     * 新增企业综合能源消费量限额
     */
    @SysLog("新增企业综合能源消费量限额")
    @PostMapping("/save")
    @ApiOperation(value = "新增企业综合能源消费量限额", notes = "新增企业综合能源消费量限额")
    @ApiImplicitParam(name = "TTce", value = "企业综合能源消费量限额", required = false, dataType = "json", paramType = "path")
    public Boolean saveTTce(@RequestBody List<TTce> tTce) {
        ScUser u = SecurityUtils.getUser();
//        tTce.setUserid(u.getId());
//        tTce.setUpdateTime(new Date());
//        tTce.setDelFlag("1");
        tTce.forEach(r->{
            r.setUserid(u.getId());
            r.setUpdateTime(new Date());
            r.setDelFlag("1");
            r.setDeptId(u.getDeptId());
        });
//        EnterpriseInfo enterpriseInfo = enterpriseInfoService.getInfoByCode(tTce.getEnterpriseCode());
//        if (enterpriseInfo != null && enterpriseInfo.getRegionCode() != null) {
//            tTce.setDeptId(Integer.parseInt(enterpriseInfo.getRegionCode())); //企业的地区码
//        } else {
//            tTce.setDeptId(u.getDeptId()); //创建用户的deptId
//        }
//        return tTceService.save(tTce);
        return tTceService.saveOrUpdateBatch(tTce);
    }

    /**
     * 修改企业综合能源消费量限额
     */
    @SysLog("修改企业综合能源消费量限额")
    @PutMapping("/update")
    @ApiOperation(value = "修改企业综合能源消费量限额", notes = "修改企业综合能源消费量限额")
    @ApiImplicitParam(name = "TTce", value = "企业综合能源消费量限额", required = false, dataType = "json", paramType = "path")
    public Boolean upTTce(@RequestBody TTce tTce) {
        ScUser u = SecurityUtils.getUser();
        tTce.setUserid(u.getId());
        tTce.setUpdateTime(new Date());
        return tTceService.updateById(tTce);
    }

    /**
     * 批量新增/修改企业综合能源消费量限额
     */
    @SysLog("批量新增/修改企业综合能源消费量限额")
    @PostMapping("/saveOrUpdateBatch")
    @ApiOperation(value = "批量更新企业综合能源消费量限额", notes = "批量更新企业综合能源消费量限额")
    @ApiImplicitParam(name = "data", value = "企业综合能源消费量限额", required = true, dataType = "String")
    public R upTTceBatch(@RequestBody String s) {

        log.error("批量更新企业综合能源消费量限额：" + s);

        JSONObject json = JSONObject.parseObject(s);
        //获取企业综合能源消费量限额
        String data = json.getString("data");
        List<TTce> tTceList = (List<TTce>) JSONObject.parseArray(data, TTce.class);
        if (tTceList.size() == 0) { //无更新数据
            return R.ok("无更新数据");
        }

        ScUser u = SecurityUtils.getUser();
        List<TTce> newList = new ArrayList<>();
        for (TTce tce : tTceList) {
            hasOld(tce, u); //判断是否有老数据，有就是修改
            tce.setUpdateTime(new Date());
            tce.setUserid(u.getId());
            newList.add(tce);
        }

        return R.ok(tTceService.saveOrUpdateBatch(newList)); //批量新增或修改
    }

    //判断是否有历史数据，有就是修改
    private void hasOld(TTce tce, ScUser u) {
        TTce t = new TTce();
        t.setEnterpriseCode(tce.getEnterpriseCode());
        t.setYear(tce.getYear());
        t.setDelFlag("1");
        TTce old = tTceService.getOne(Wrappers.query(t));
        if (old != null) { //修改
            tce.setId(old.getId());
        } else { //新增
            tce.setId(null);
            tce.setDelFlag("1");
            EnterpriseInfo enterpriseInfo = enterpriseInfoService.getInfoByCode(tce.getEnterpriseCode());
            if (enterpriseInfo != null && enterpriseInfo.getRegionCode() != null) {
                tce.setDeptId(Integer.parseInt(enterpriseInfo.getRegionCode())); //企业的地区码
            } else {
                tce.setDeptId(u.getDeptId()); //创建用户的deptId
            }
        }
    }

}
