package com.nantian.cloud.jljc.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.entity.EnergyInfo;
import com.nantian.cloud.jljc.service.EnergyInfoService;
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;
import java.util.List;


/**
 * 能源信息
 *
 * @author
 * @date 2018-09-27 15:20:44
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/energyInfo")
@Api(value = "energyInfo", tags = "能源信息")
public class EnergyInfoController {

    private final EnergyInfoService energyInfoService;


    /**
     * 分页查询
     *
     * @param page       分页对象
     * @param energyInfo
     * @return
     */
    @GetMapping("/page")
    @SysLog("分页获取能源信息")
    @ApiOperation(value = "分页获取能源信息", notes = "分页获取能源信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "energyInfo", value = "能源信息对象（查询条件：能源名称energyName）", required = false, dataType = "json", paramType = "path")
    })
    public R getEnergyInfoPage(Page page, EnergyInfo energyInfo) {
        //return R.ok(energyInfoService.page(page, Wrappers.query(energyInfo)));
        energyInfo.setDelFlag("1"); //筛选未删除项
        return R.ok(energyInfoService.getPageScope(page, energyInfo));
    }

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

    @GetMapping("/tree")
    @ApiOperation(value = "获取能源信息树形结构", notes = "获取能源信息树形结构")
    public R getEnergyInfoTree() {
        return R.ok(energyInfoService.getEnergyInfoTree());
    }

    /**
     * 保存
     *
     * @param energyInfo
     * @return R
     */
    @PostMapping
    @SysLog("新增能源信息")
    @ApiOperation(value = "新增能源信息", notes = "新增能源信息")
    @ApiImplicitParam(name = "EnergyInfo", value = "能源信息对象", required = true, dataType = "json", paramType = "path")
    public R save(@RequestBody EnergyInfo energyInfo) {
        if (energyInfo.getCode() == null) {
            return R.failed("能源品种编码不能为空！");
        }
        EnergyInfo o = new EnergyInfo();
        o.setCode(energyInfo.getCode());
        if (energyInfoService.list(Wrappers.query(o)).size() > 0) {
            return R.failed("能源品种编码已存在，新增能源信息失败！");
        }
        ScUser u = SecurityUtils.getUser();
        energyInfo.setUserid(u.getId());
        energyInfo.setDeptId(u.getDeptId());
        energyInfo.setUpdateTime(new Date());
        energyInfo.setDelFlag("1");
        return R.ok(energyInfoService.save(energyInfo));
    }

    /**
     * 修改
     *
     * @param energyInfo
     * @return R
     */
    @PutMapping
    @SysLog("修改能源信息")
    @ApiOperation(value = "修改能源信息", notes = "修改能源信息")
    @ApiImplicitParam(name = "EnergyInfo", value = "能源信息对象", required = true, dataType = "json", paramType = "path")
    public R updateById(@RequestBody EnergyInfo energyInfo) {
        if (energyInfo.getCode() != null) {
            EnergyInfo o = new EnergyInfo();
            o.setCode(energyInfo.getCode());
            List<EnergyInfo> list = energyInfoService.list(Wrappers.query(o));
            if (list.size() > 0  && energyInfo.getId().longValue() != list.get(0).getId().longValue()) {
                return R.failed("能源品种编码已存在，修改能源信息失败！");
            }
        }
        ScUser u = SecurityUtils.getUser();
        energyInfo.setUserid(u.getId());
        energyInfo.setUpdateTime(new Date());
        return R.ok(energyInfoService.updateById(energyInfo));
    }

    /**
     * 删除
     *
     * @param energyInfoId
     * @return R
     */
    @DeleteMapping("/{energyInfoId}")
    @SysLog("删除能源信息")
    @ApiOperation(value = "删除能源信息", notes = "删除能源信息")
    @ApiImplicitParam(name = "energyInfoId", value = "id", required = true, dataType = "int", paramType = "path")
    public R removeById(@PathVariable Integer energyInfoId) {
        EnergyInfo energyInfo = energyInfoService.getById(energyInfoId);
        ScUser u = SecurityUtils.getUser();
        energyInfo.setUserid(u.getId());
        energyInfo.setUpdateTime(new Date());
        energyInfo.setDelFlag("4");
        return R.ok(energyInfoService.updateById(energyInfo));
    }

    /**
     * 获取所有能源信息列表
     */
    @GetMapping("/list")
    @SysLog("获取所有能源信息列表")
    @ApiOperation(value = "获取所有能源信息列表", notes = "获取所有能源信息列表")
    @ApiImplicitParam(name = "energyInfo", value = "能源信息对象", required = false,  paramType = "path")
    public R getEnergyInfoList() {
        return R.ok(energyInfoService.getEnergyInfoList());
    }

}
