package com.nantian.cloud.jljc.controller;

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.jljc.entity.EcAbnormalVolatility;
import com.nantian.cloud.jljc.entity.EcAbnormalVolatilityInfo;
import com.nantian.cloud.jljc.entity.EnterpriseInfo;
import com.nantian.cloud.jljc.entity.TReportEnterprise;
import com.nantian.cloud.jljc.service.EcAbnormalVolatilityInfoService;
import com.nantian.cloud.jljc.service.EcAbnormalVolatilityService;
import com.nantian.cloud.jljc.service.EnterpriseInfoService;
import com.nantian.cloud.jljc.service.TReportEnterpriseService;
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 lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.List;

/**
 * 企业能耗异常波动详细信息表
 *
 * @author
 * @date 2018-09-27 15:20:44
 */
@RestController
@AllArgsConstructor
@RequestMapping("/ecAbnormalVolatilityInfo")
@CrossOrigin
@Api(value = "ecAbnormalVolatilityInfo", tags = "企业能耗异常波动详细信息表")
public class EcAbnormalVolatilityInfoController {
    private final EcAbnormalVolatilityInfoService ecAbnormalVolatilityInfoService;
    private final EcAbnormalVolatilityService ecAbnormalVolatilityService;
    private final TReportEnterpriseService tReportEnterpriseService;
    private final EnterpriseInfoService enterpriseInfoService;

    @PostMapping("/addByReport")
    @SysLog("（填写能报）新增企业能耗异常波动详细信息表")
    @ApiOperation(value = "（填写能报）新增企业能耗异常波动详细信息表", notes = "（填写能报）新增企业能耗异常波动详细信息表")
    @ApiImplicitParam(name = "ecAbnormalVolatilityInfo", value = "企业能耗异常波动详细信息表对象", required = true, dataType = "json", paramType = "path")
    public R addByReport(@RequestBody EcAbnormalVolatilityInfo ecAbnormalVolatilityInfo) {
        if (ecAbnormalVolatilityInfo.getYear() == null) {
            return R.failed("年份不能为空");
        }

        //企业新增异常波动数据，无deptId，从企业信息取出
        Integer deptId = 0;
        ScUser u = SecurityUtils.getUser();
        EcAbnormalVolatility ecAbnormalVolatility = new EcAbnormalVolatility();
        ecAbnormalVolatility.setYear(ecAbnormalVolatilityInfo.getYear());
        ecAbnormalVolatility.setEpc(u.getEnterpriseCode());
        ecAbnormalVolatility.setDelFlag("1");
        EcAbnormalVolatility old = ecAbnormalVolatilityService.getOne(Wrappers.query(ecAbnormalVolatility));
        if (old != null) {
            ecAbnormalVolatilityInfo.setAbnormalVolatilityId(old.getId());  //设置异常波动主表id
        } else {
            TReportEnterprise enterprise = tReportEnterpriseService.getEnterpriseInfo(u.getEnterpriseCode(), ecAbnormalVolatilityInfo.getYear());
            if (enterprise != null) {
                deptId = enterprise.getArea()!=null ? Integer.parseInt(enterprise.getArea()) : u.getDeptId();
                //return R.failed(ecAbnormalVolatilityInfo.getYear()+"年 报表设置【企业基本信息】未完成，请前去设置");
                TReportEnterprise nameByEnterprise = tReportEnterpriseService.getNameByEnterprise(enterprise);
                //ecAbnormalVolatility.setEpcName(nameByEnterprise.getEnterpriseName());
                ecAbnormalVolatility.setArea(nameByEnterprise.getParea());
                ecAbnormalVolatility.setArea2(nameByEnterprise.getArea());
                ecAbnormalVolatility.setTradeNum(nameByEnterprise.getPtrade());
                ecAbnormalVolatility.setTradeNum2(nameByEnterprise.getTrade());
            } else {
                EnterpriseInfo enterpriseInfo = enterpriseInfoService.getInfoNameByCode(u.getEnterpriseCode());
                if(enterpriseInfo!=null){
                    deptId = enterpriseInfo.getRegionCode() != null ? Integer.parseInt(enterpriseInfo.getRegionCode()) : u.getDeptId();
                    //ecAbnormalVolatility.setEpcName(enterpriseInfo.getCmpName());
                    ecAbnormalVolatility.setArea2(enterpriseInfo.getRegionName());
                    ecAbnormalVolatility.setTradeNum2(enterpriseInfo.getIndustryName());
                }else{
                    deptId = u.getDeptId();
                }
            }
            ecAbnormalVolatility.setUpdateTime(new Date());
            //ecAbnormalVolatility.setDeptId(u.getDeptId());
            ecAbnormalVolatility.setDeptId(deptId);
            ecAbnormalVolatility.setUserid(u.getId());
            ecAbnormalVolatility.setDataStatus("2"); //异常
            boolean b = ecAbnormalVolatilityService.save(ecAbnormalVolatility);
            if (b) {
                ecAbnormalVolatilityInfo.setAbnormalVolatilityId(ecAbnormalVolatility.getId());  //设置异常波动主表id
            }
        }

        EcAbnormalVolatilityInfo oldInfo = new EcAbnormalVolatilityInfo();
        oldInfo.setDelFlag("1");
        oldInfo.setAbnormalVolatilityId(ecAbnormalVolatilityInfo.getId());
        oldInfo.setDataTable(ecAbnormalVolatilityInfo.getDataTable());
        oldInfo.setDataContent(ecAbnormalVolatilityInfo.getDataContent());
        EcAbnormalVolatilityInfo info = ecAbnormalVolatilityInfoService.getOne(Wrappers.query(oldInfo));
        if (info != null) { //修改
            ecAbnormalVolatilityInfo.setId(info.getId());
        } else {
            //ecAbnormalVolatilityInfo.setDeptId(u.getDeptId());
            ecAbnormalVolatilityInfo.setDeptId(deptId);
            ecAbnormalVolatilityInfo.setDelFlag("1");
        }

        ecAbnormalVolatilityInfo.setUserid(u.getId());
        ecAbnormalVolatilityInfo.setUpdateTime(new Date());
        return R.ok(ecAbnormalVolatilityInfoService.saveOrUpdate(ecAbnormalVolatilityInfo));
    }


    /**
     * （报表预览）获取企业能耗异常波动详细信息列表
     */
    @GetMapping("/listByReport")
    @SysLog("（报表预览）获取企业能耗异常波动详细信息列表")
    @ApiOperation(value = "（报表预览）获取企业能耗异常波动详细信息列表", notes = "（报表预览）获取企业能耗异常波动详细信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年份", required = true, paramType = "path"),
            @ApiImplicitParam(name = "enterpriseCode", value = "企业编码", required = true, paramType = "path"),
            @ApiImplicitParam(name = "dataTable", value = "数据表类型（表1、表2、表2-1、表3、表4、表5）", required = true, paramType = "path")
    })
    public R<List<EcAbnormalVolatilityInfo>> listByReport(String year, String enterpriseCode, String dataTable) {
        if (year == null || "".equals(year)) {
            return R.failed("获取能耗异常波动详细信息列表失败，年度不能为空");
        }
        if (enterpriseCode == null || "".equals(enterpriseCode)) {
            return R.failed("获取能耗异常波动详细信息列表失败，企业社会信用编码不能为空");
        }
        if (dataTable == null || "".equals(dataTable)) {
            return R.failed("获取能耗异常波动详细信息列表失败，数据表类型不能为空");
        }

        EcAbnormalVolatility ecAbnormalVolatility = new EcAbnormalVolatility();
        ecAbnormalVolatility.setYear(year);
        ecAbnormalVolatility.setEpc(enterpriseCode);
        ecAbnormalVolatility.setDelFlag("1");
        EcAbnormalVolatility volatility = ecAbnormalVolatilityService.getOne(Wrappers.query(ecAbnormalVolatility));
        if (volatility == null) {
            return R.failed();
        }

        EcAbnormalVolatilityInfo ecAbnormalVolatilityInfo = new EcAbnormalVolatilityInfo();
        ecAbnormalVolatilityInfo.setAbnormalVolatilityId(volatility.getId());
        ecAbnormalVolatilityInfo.setDataTable(dataTable);
        ecAbnormalVolatilityInfo.setDelFlag("1");
        return R.ok(ecAbnormalVolatilityInfoService.list(Wrappers.query(ecAbnormalVolatilityInfo)));
    }


    /**
     * 分页查询
     *
     * @param page                     分页对象
     * @param ecAbnormalVolatilityInfo
     * @return
     */
    @GetMapping("/page")
    @SysLog("分页获取企业能耗异常波动详细信息表")
    @ApiOperation(value = "分页获取企业能耗异常波动详细信息表", notes = "分页获取企业能耗异常波动详细信息表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "ecAbnormalVolatilityInfo", value = "企业能耗异常波动详细信息表对象", required = false, dataType = "json", paramType = "path")
    })
    public R getEcAbnormalVolatilityInfoPage(Page page, EcAbnormalVolatilityInfo ecAbnormalVolatilityInfo) {
        ecAbnormalVolatilityInfo.setDelFlag("1");  //筛选未删除项
        return R.ok(ecAbnormalVolatilityInfoService.page(page, Wrappers.query(ecAbnormalVolatilityInfo)));
    }


    /**
     * 信息
     *
     * @param ecAbnormalVolatilityInfoId
     * @return R
     */
    @GetMapping("/{ecAbnormalVolatilityInfoId}")
    @SysLog("根据id获取企业能耗异常波动详细信息表")
    @ApiOperation(value = "根据id获取企业能耗异常波动详细信息表", notes = "根据id获取企业能耗异常波动详细信息表")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
    public R getById(@PathVariable("ecAbnormalVolatilityInfoId") Integer ecAbnormalVolatilityInfoId) {
        return R.ok(ecAbnormalVolatilityInfoService.getById(ecAbnormalVolatilityInfoId));
    }

    /**
     * 保存
     *
     * @param ecAbnormalVolatilityInfo
     * @return R
     */
    @PostMapping
    @SysLog("新增企业能耗异常波动详细信息表")
    @ApiOperation(value = "新增企业能耗异常波动详细信息表", notes = "新增企业能耗异常波动详细信息表")
    @ApiImplicitParam(name = "ecAbnormalVolatilityInfo", value = "企业能耗异常波动详细信息表对象", required = true, dataType = "json", paramType = "path")
    public R save(@RequestBody EcAbnormalVolatilityInfo ecAbnormalVolatilityInfo) {
        ScUser u = SecurityUtils.getUser();
        ecAbnormalVolatilityInfo.setUserid(u.getId());
        ecAbnormalVolatilityInfo.setDeptId(u.getDeptId());
        ecAbnormalVolatilityInfo.setUpdateTime(new Date());
        ecAbnormalVolatilityInfo.setDelFlag("1");
        return R.ok(ecAbnormalVolatilityInfoService.save(ecAbnormalVolatilityInfo));
    }

    /**
     * 修改
     *
     * @param ecAbnormalVolatilityInfo
     * @return R
     */
    @PutMapping
    @SysLog("修改企业能耗异常波动详细信息表")
    @ApiOperation(value = "修改企业能耗异常波动详细信息表", notes = "修改企业能耗异常波动详细信息表")
    @ApiImplicitParam(name = "ecAbnormalVolatilityInfo", value = "企业能耗异常波动详细信息表对象", required = true, dataType = "json", paramType = "path")
    public R updateById(@RequestBody EcAbnormalVolatilityInfo ecAbnormalVolatilityInfo) {
        ScUser u = SecurityUtils.getUser();
        ecAbnormalVolatilityInfo.setUserid(u.getId());
        ecAbnormalVolatilityInfo.setUpdateTime(new Date());
        return R.ok(ecAbnormalVolatilityInfoService.updateById(ecAbnormalVolatilityInfo));
    }

    /**
     * 删除
     *
     * @param ecAbnormalVolatilityInfoId
     * @return R
     */
    @DeleteMapping("/{ecAbnormalVolatilityInfoId}")
    @SysLog("删除企业能耗异常波动详细信息表")
    @ApiOperation(value = "删除企业能耗异常波动详细信息表", notes = "删除企业能耗异常波动详细信息表")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
    public R removeById(@PathVariable Integer ecAbnormalVolatilityInfoId) {
        EcAbnormalVolatilityInfo ecAbnormalVolatilityInfo = ecAbnormalVolatilityInfoService.getById(ecAbnormalVolatilityInfoId);
        ScUser u = SecurityUtils.getUser();
        ecAbnormalVolatilityInfo.setUserid(u.getId());
        ecAbnormalVolatilityInfo.setUpdateTime(new Date());
        ecAbnormalVolatilityInfo.setDelFlag("4");
        return R.ok(ecAbnormalVolatilityInfoService.updateById(ecAbnormalVolatilityInfo));
    }

    /**
     * @return
     */
    @GetMapping("/detail/{aid}")
    @SysLog("获取企业能耗异常波动详细明细信息")
    @ApiOperation(value = "获取企业能耗异常波动详细明细信息", notes = "获取企业能耗异常波动详细明细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "aid", value = "异常波动id", required = true)
    })
    public R getEcAbnormalVolatilityInfoDetail(@PathVariable("aid") Integer aid) {
        return R.ok(ecAbnormalVolatilityInfoService.getByAid(aid));
    }

}
