package com.nantian.cloud.dbcapp.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nantian.cloud.dbcapp.entity.AccessProductQuota;
import com.nantian.cloud.dbcapp.service.AccessProductQuotaService;
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.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/accessProductQuota")
@Api(value = "accessProductQuota", tags = "延期接入企业-产品指标信息")
public class AccessProductQuotaController {

    @Autowired
    private AccessProductQuotaService quotaService;

    @GetMapping("/list")
    @SysLog("获取产品指标信息")
    @ApiOperation(value = "获取产品指标信息", notes = "获取产品指标信息")
    public R list(String epc, String date) {
        QueryWrapper q1 = new QueryWrapper<AccessProductQuota>()
                .eq("del_flag","1")
                .like("enterprise_code", epc==null ? "" :epc)
                .like("record_date", date==null ? "" :date)
                .notLike("product_name","企业综合能耗");
        QueryWrapper q2 = new QueryWrapper<AccessProductQuota>()
                .eq("del_flag","1")
                .like("enterprise_code", epc==null ? "" :epc)
                .like("record_date", date==null ? "" :date)
                .like("product_name","企业综合能耗");
        Map<String,Object> map = new HashMap<>();
        map.put("list",quotaService.list(q1));
        map.put("tce",quotaService.getOne(q2));
        return R.ok(map);
    }

    @PostMapping("/renewParamBatch")
    @SysLog("更新产品指标信息")
    @ApiOperation(value = "更新产品指标信息", notes = "更新产品指标信息")
    public R saveOrUpdateBatch(@RequestBody String str) {
        JSONObject json = JSONObject.parseObject(str);
        String epc = json.getString("epc");
        String date = json.getString("date");
        if(epc==null || date==null){
            return R.failed("企业编码或统计周期不能为空！");
        }
        List<AccessProductQuota> dataList = (List<AccessProductQuota>) JSONArray.parseArray(json.getString("list"), AccessProductQuota.class);
        ScUser u = SecurityUtils.getUser();
        for (AccessProductQuota quota : dataList) {
            if(quota.getId()==null){
                quota.setDelFlag("1");
            }
            quota.setEnterpriseCode(epc.toString());
            quota.setRecordDate(date.toString());
            quota.setUpdateTime(new Date());
            quota.setDeptId(u.getDeptId());
            quota.setUserid(u.getId());
            quotaService.saveOrUpdate(quota);
        }
        return  R.ok("更新产品指标信息成功！");
    }

    @DeleteMapping("/delete")
    @SysLog("根据id删除产品指标信息")
    @ApiOperation(value = "根据id删除产品指标信息", notes = "根据id删除产品指标信息")
    public R removeById(Integer id) {
        AccessProductQuota quota = quotaService.getById(id);
        if (quota == null) {
            return R.failed("产品指标信息不存在，删除失败！");
        }
        ScUser u = SecurityUtils.getUser();
        quota.setUserid(u.getId());
        quota.setUpdateTime(new Date());
        quota.setDelFlag("4");
        return R.ok(quotaService.updateById(quota));
    }
}
