package com.nantian.cloud.jljc.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.entity.EnergyIndex;
import com.nantian.cloud.jljc.service.EnergyIndexService;
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 org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * 耗能指标信息
 *
 * @author
 * @date 2018-09-27 15:20:44
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/energyIndex")
@Api(value = "energyIndex", tags = "耗能指标信息")
public class EnergyIndexController {
    private final EnergyIndexService energyIndexService;


    /**
     * 分页查询
     *
     * @param page        分页对象
     * @param energyIndex
     * @return
     */
    @GetMapping("/page")
    @SysLog("分页获取耗能指标产品信息")
    @ApiOperation(value = "分页获取耗能指标产品信息", notes = "分页获取耗能指标产品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "energyIndex", value = "耗能指标信息对象（查询条件：指标名称indexName、行业代码tradeProductCode、" +
                    "行业名称tradeProductName、产品代码productInfoNum、产品名称productInfoName）", required = false, dataType = "json", paramType = "path")
    })
    public R getEnergyIndexPage(Page page, EnergyIndex energyIndex) {
        //return R.ok(energyIndexService.page(page, Wrappers.query(energyIndex)));
        energyIndex.setDelFlag("1"); //筛选未删除项
        return R.ok(energyIndexService.getPageScope(page, energyIndex));
    }

    @GetMapping("/pageCode")
    @SysLog("分页获取耗能指标信息")
    @ApiOperation(value = "分页获取耗能指标信息", notes = "分页获取耗能指标信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "energyIndex", value = "耗能指标信息对象（查询条件：指标名称indexName、行业代码tradeProductCode、" +
                    "行业名称tradeProductName、产品代码productInfoNum、产品名称productInfoName）", required = false, dataType = "json", paramType = "path")
    })
    public R getEnergyIndexPageCode(Page page, EnergyIndex energyIndex) {
        //return R.ok(energyIndexService.page(page, Wrappers.query(energyIndex)));
        energyIndex.setDelFlag("1"); //筛选未删除项
        return R.ok(energyIndexService.getPageScopeCode(page, energyIndex));
    }

    /**
     * 信息
     *
     * @param energyIndexId
     * @return R
     */
    @GetMapping("/{energyIndexId}")
    @SysLog("根据id获取耗能指标信息")
    @ApiOperation(value = "根据id获取耗能指标信息", notes = "根据id获取耗能指标信息")
    @ApiImplicitParam(name = "energyIndexId", value = "id", required = true, dataType = "int", paramType = "path")
    public R getById(@PathVariable("energyIndexId") Integer energyIndexId) {
        return R.ok(energyIndexService.getById(energyIndexId));
    }

    /**
     * 根据产品编码获取耗能指标信息
     */
    @GetMapping("/productNum/{productNum}")
    @SysLog("根据产品编码获取耗能指标信息")
    @ApiOperation(value = "根据产品编码获取耗能指标信息", notes = "根据产品编码获取耗能指标信息")
    @ApiImplicitParam(name = "productNum", value = "产品编码", required = true, dataType = "String", paramType = "path")
    public R getByProductNum(@PathVariable("productNum") String productNum) {
        return R.ok(energyIndexService.getByProductNum(productNum));
    }

    /**
     * 保存
     *
     * @param energyIndex
     * @return R
     */
    @PostMapping
    @SysLog("新增耗能指标信息")
    @ApiOperation(value = "新增耗能指标信息", notes = "新增耗能指标信息")
    @ApiImplicitParam(name = "EnergyIndex", value = "耗能指标信息对象", required = true, dataType = "json", paramType = "path")
    public R save(@RequestBody EnergyIndex energyIndex) {
        ScUser u= SecurityUtils.getUser();
        energyIndex.setUserid(u.getId());
        energyIndex.setDeptId(u.getDeptId());
        energyIndex.setUpdateTime(new Date());
        energyIndex.setDelFlag("1");
        return R.ok(energyIndexService.save(energyIndex));
    }

    /**
     * 修改
     *
     * @param energyIndex
     * @return R
     */
    @PutMapping
    @SysLog("修改耗能指标信息")
    @ApiOperation(value = "修改耗能指标信息", notes = "修改耗能指标信息")
    @ApiImplicitParam(name = "EnergyIndex", value = "耗能指标信息对象", required = true, dataType = "json", paramType = "path")
    public R updateById(@RequestBody EnergyIndex energyIndex) {
        ScUser u= SecurityUtils.getUser();
        energyIndex.setUserid(u.getId());
        energyIndex.setUpdateTime(new Date());
        return R.ok(energyIndexService.updateById(energyIndex));
    }

    /**
     * 删除
     *
     * @param energyIndexId
     * @return R
     */
    @DeleteMapping("/{energyIndexId}")
    @SysLog("删除耗能指标信息")
    @ApiOperation(value = "删除耗能指标信息", notes = "删除耗能指标信息")
    @ApiImplicitParam(name = "energyIndexId", value = "id", required = true, dataType = "string", paramType = "path")
    public R removeById(@PathVariable String energyIndexId) {
        EnergyIndex energyIndex = energyIndexService.getById(energyIndexId);
        if(energyIndex==null){
            return R.failed("耗能指标信息不存在，删除失败");
        }
        ScUser u= SecurityUtils.getUser();
        energyIndex.setUserid(u.getId());
        energyIndex.setUpdateTime(new Date());
        energyIndex.setDelFlag("4");
        return R.ok(energyIndexService.updateById(energyIndex));
    }

    /**
     * 条件查询
     *
     * @param energyIndex
     * @return R
     */
    @GetMapping("/selectall")
    @SysLog("条件查询耗能指标信息")
    @ApiOperation(value = "条件查询耗能指标信息", notes = "条件查询耗能指标信息")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
    public R selectall(Page page,EnergyIndex energyIndex) {
        return R.ok(energyIndexService.selectall(page,energyIndex));
    }

    @GetMapping("/listall")
    @SysLog("查询耗能指标信息集合")
    public R listAll(){
        return R.ok(energyIndexService.getEnergyIndexList());
    }
}
