package com.nantian.cloud.jljc.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.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nantian.cloud.jljc.entity.TReportPara;
import com.nantian.cloud.jljc.entity.TSupervisionEnergy;
import com.nantian.cloud.jljc.service.TReportParaService;
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.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

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


/**
 * 能报其他设置参数表
 *
 * @since 2020-10-28
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/tReportPara")
@Api(value = "tReportPara", tags = "能报其他设置参数表")
public class TReportParaController {

    private final TReportParaService tReportParaService;

    /**
     * 查询全部能报其他设置参数表
     */
    @GetMapping("/list")
    @SysLog("查询全部能报其他设置参数表")
    @ApiOperation(value = "查询全部能报其他设置参数表", notes = "查询全部能报其他设置参数表")
    @ApiImplicitParam(name = "tReportPara", value = "能报其他设置参数表对象", required = false, dataType = "json", paramType = "path")
    public R selectAll(TReportPara tReportPara) {
        tReportPara.setDelFlag("1"); //筛选未删除项
        return R.ok(tReportParaService.list(Wrappers.query(tReportPara)));
    }

    /**
     * 根据id获取能报其他设置参数表
     */
    @GetMapping("/getById")
    @SysLog("根据id获取能报其他设置参数表")
    @ApiOperation(value = "根据id获取能报其他设置参数表", notes = "根据id获取能报其他设置参数表")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
    public R getById(Integer id) {
        return R.ok(tReportParaService.getById(id));
    }

    /**
     * 保存/修改能报其他设置参数表
     */
    @PostMapping("saveOrUpdate")
    @SysLog("保存/修改能报其他设置参数表")
    @ApiOperation(value = "保存/修改能报其他设置参数表", notes = "保存/修改能报其他设置参数表")
    @ApiImplicitParam(name = "tReportPara", value = "能报其他设置参数表对象", required = true, dataType = "json", paramType = "path")
    public R saveOrUpdate(@RequestBody TReportPara tReportPara) {
        ScUser u = SecurityUtils.getUser();
        tReportPara.setUserid(u.getId());
        tReportPara.setUpdateTime(new Date());
        if(tReportPara.getId()!=null){
            tReportPara.setDelFlag("1");
            tReportPara.setDeptId(u.getDeptId());
        }
        return R.ok(tReportParaService.saveOrUpdate(tReportPara));
    }



    /**
     * 批量保存/修改能报其他设置参数表
     */
    @PostMapping("saveOrUpdateBatch")
    @SysLog("批量保存/修改能报其他设置参数表")
    @ApiOperation(value = "批量保存/修改能报其他设置参数表", notes = "批量保存/修改能报其他设置参数表")
    @ApiImplicitParam(name = "data", value = "能报其他设置参数表对象", required = true, dataType = "json", paramType = "path")
    public R saveOrUpdateBatch(@RequestBody String s) {
        JSONObject json = JSONObject.parseObject(s);
        String para = json.getString("data");
        List<TReportPara> tReportParaList = (List<TReportPara>) JSONArray.parseArray(para, TReportPara.class);
        List<TReportPara> newList = new ArrayList<>();
        for (TReportPara tReportPara : tReportParaList) {
            ScUser u = SecurityUtils.getUser();
            tReportPara.setUserid(u.getId());
            tReportPara.setUpdateTime(new Date());
            if(tReportPara.getId()!=null){
                tReportPara.setDelFlag("1");
                tReportPara.setDeptId(u.getDeptId());
            }
            newList.add(tReportPara);
        }
        return R.ok(tReportParaService.saveOrUpdateBatch(newList));
    }


    @PostMapping("/saveOrUpdateFocusAreas")
    @SysLog("更新能报关注的地区")
    @ApiOperation(value = "更新能报关注的地区")
    public R saveOrUpdateFocusAreas(@RequestBody String reportPara){
        ScUser u = SecurityUtils.getUser();
        TReportPara tReportPara =  JSONObject.parseObject(reportPara, TReportPara.class);
        TReportPara oldPara = tReportParaService.getOne(new QueryWrapper<TReportPara>().eq("del_flag","1").eq("userid",u.getId()).eq("type","4"));
        if(oldPara != null){
            tReportPara.setId(oldPara.getId());
        }else{
            tReportPara.setType("4");
            tReportPara.setDelFlag("1");
            tReportPara.setUserid(u.getId());
            tReportPara.setDeptId(u.getDeptId());
            tReportPara.setName("用户所关注的报表地区");
        }
        tReportPara.setUpdateTime(new Date());
        return R.ok(tReportParaService.saveOrUpdate(tReportPara));
    }

    @GetMapping("/getFocusList")
    @ApiOperation(value = "查询能报关注的地区列表")
    public R getFocusList(String year){
        ScUser u = SecurityUtils.getUser();
        QueryWrapper<TReportPara> query = new QueryWrapper<>();
        query.eq("type", 4); //类型(1:企业能耗异常波动设置，2:企业数据平衡性设置，3:能耗级别设置参数，4:用户所关注的报表地区)
        query.eq("del_flag", 1); //未删除
        query.eq("userid",u.getId());
        query.like(StringUtils.isNotEmpty(year),"year", year);
        query.orderByAsc("year");
        return R.ok(tReportParaService.list(query));
    }

    @GetMapping("/removeFocus")
    @ApiOperation(value = "删除能报关注的地区")
    public R removeFocus(Integer id){
        TReportPara para = tReportParaService.getById(id);
        if (para == null) {
            return R.failed("id不存在，删除失败！");
        }
        para.setDelFlag("4");
        para.setUpdateTime(new Date());
        return R.ok(tReportParaService.updateById(para));
    }

}
