package com.nantian.cloud.ggjg.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.ggjg.entity.TQuotaManage;
import com.nantian.cloud.ggjg.service.TQuotaManageService;
import com.nantian.sc.cloud.common.core.util.R;
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.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * 能耗定额指标管理
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/tQuotaManage")
@Api(value = "tQuotaManage", tags = "能耗定额指标管理")
public class TQuotaManageController {

    final private TQuotaManageService tQuotaManageService;

    /**
     *  能耗定额指标管理分页查询
     * @param page      分页对象
     * @param tQuotaManage  能耗定额指标管理
     * @return
     */
    @ApiOperation(value = "查询能耗定额指标",notes = "条件查询能耗定额指标")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tQuotaManage", value = " 能耗定额指标", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "Page", value = "分页对象", required = false, dataType = "json", paramType = "path")
    })
    @GetMapping("/queryList")
    public R queryList(Page page, TQuotaManage tQuotaManage) {
        tQuotaManage.setDelFlag("1");
        return R.ok(tQuotaManageService.page(page,Wrappers.query(tQuotaManage)));
    }


    /**
     * 信息
     */
    @ApiOperation(value = "获取能耗定额指标", notes = "获取能耗定额指标")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        TQuotaManage o = tQuotaManageService.getById(id);
        return R.ok(o);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "增加能耗定额指标", notes = "增加能耗定额指标")
    @PostMapping("/save")
    @ApiImplicitParam(name = "TQuotaManage", value = "增加能耗定额指标", required = true, dataType = "json", paramType = "path")
    public R save(@RequestBody TQuotaManage tQuotaManage) {
        ScUser u = SecurityUtils.getUser();
        tQuotaManage.setDeptId(u.getDeptId());
        tQuotaManage.setUserid(u.getId());
        tQuotaManage.setUpdateTime(new Date());
        TQuotaManage t = new TQuotaManage();
        t.setArea(tQuotaManage.getArea());
        t.setType(tQuotaManage.getType());
        t.setKind(tQuotaManage.getKind());
        t.setIndicators(tQuotaManage.getIndicators());
        if("行政机关".equals(tQuotaManage.getType())){
            if(StringUtils.isBlank(tQuotaManage.getArea())){
                return R.failed("行政机关区域不能为空，请选择！");
            }
            t.setArea(tQuotaManage.getArea());
        }
        if(tQuotaManageService.count(Wrappers.query(t))!=0){
            return R.failed("能耗定额指标已经存在，请检查！");
        }
        return R.ok(tQuotaManageService.save(tQuotaManage));
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新能耗定额指标", notes = "更新能耗定额指标")
    @PutMapping("/update")
    public R update(@RequestBody TQuotaManage tQuotaManage) {
        ScUser u = SecurityUtils.getUser();
        tQuotaManage.setDeptId(u.getDeptId());
        tQuotaManage.setUserid(u.getId());
        tQuotaManage.setUpdateTime(new Date());
        return R.ok(tQuotaManageService.updateById(tQuotaManage));
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除能耗定额指标", notes = "删除能耗定额指标")
    @DeleteMapping("/delete")
    public R delete(Integer id) {
        TQuotaManage tQuotaManage = tQuotaManageService.getById(id);
        if (tQuotaManage == null) {
            return R.failed("能耗定额指标信息不存在，删除失败！");
        }
        ScUser u = SecurityUtils.getUser();
        tQuotaManage.setUserid(u.getId());
        tQuotaManage.setUpdateTime(new Date());
        tQuotaManage.setDelFlag("4");
        return R.ok(tQuotaManageService.updateById(tQuotaManage));
    }
}
