package com.nantian.cloud.jljc.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.entity.TradeInfo;
import com.nantian.cloud.jljc.entity.TradeKind;
import com.nantian.cloud.jljc.service.TradeInfoService;
import com.nantian.cloud.jljc.service.TradeKindService;
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 lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.math.BigInteger;
import java.util.Date;

/**
 * 行业信息（节能）
 *
 * @author
 * @date 2018-09-27 15:20:44
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/tradeInfo")
@Api(value = "tradeInfo",tags = "行业信息（节能）")
public class TradeInfoController {
	private final TradeInfoService tradeInfoService;
	private final TradeKindService tradeKindService;


	/**
	 * 分页查询
	 *
	 * @param page      分页对象
	 * @param tradeInfo
	 * @return
	 */
	@GetMapping("/page")
	@ApiOperation(value = "分页获取行业信息（节能）", notes = "分页获取行业信息（节能）")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
				@ApiImplicitParam(name = "tradeInfo", value = "行业信息（节能）对象（查询条件：行业代码tradeNum、行业名称tradeName、归类编码kindCode、归类名称kindName）", required = false, dataType = "json", paramType = "path")
	})
	public R getTradeInfoPage(Page page, TradeInfo tradeInfo) {
		//return R.ok(tradeInfoService.page(page, Wrappers.query(tradeInfo)));
		tradeInfo.setDelFlag("1");  //筛选未删除项
		return R.ok(tradeInfoService.getPageScope(page,tradeInfo));
	}


	/**
	 * 信息
	 *
	 * @param tradeInfoId
	 * @return R
	 */
	@GetMapping("/{tradeInfoId}")
	@ApiOperation(value = "根据id获取行业信息（节能）", notes = "根据id获取行业信息（节能）")
	@ApiImplicitParam(name = "tradeInfoId", value = "id", required = true, dataType = "int", paramType = "path")
	public R getById(@PathVariable("tradeInfoId") BigInteger tradeInfoId) {
		return R.ok(tradeInfoService.getById(tradeInfoId));
	}

	/**
	 * 保存
	 *
	 * @param tradeInfo
	 * @return R
	 */
	@PostMapping
	@SysLog("新增行业信息（节能）")
	@ApiOperation(value = "新增行业信息（节能）", notes = "新增行业信息（节能）")
	@ApiImplicitParam(name = "TradeInfo", value = "行业信息（节能）对象", required = true, dataType = "json", paramType = "path")
	public R save(@RequestBody TradeInfo tradeInfo) {
		ScUser u= SecurityUtils.getUser();
		tradeInfo.setUserid(u.getId());
		tradeInfo.setDeptId(u.getDeptId());
		tradeInfo.setUpdateTime(new Date());
		tradeInfo.setDelFlag("1");

		TradeKind k = new TradeKind();
		if(tradeInfo.getTradeNum()!=null){
			k.setCode(tradeInfo.getTradeNum());
			tradeInfo.setTradeName(tradeKindService.selectbycode(k).getName());
		}
		if(tradeInfo.getKind()!=null){
			k.setCode(tradeInfo.getKind());
			TradeKind tradeKind = tradeKindService.selectbycode(k);
			if(tradeKind!=null){
				tradeInfo.setKind(tradeInfo.getKind()+":"+tradeKind.getName());
			}
		}
		if(tradeInfo.getBigKind()!=null){
			k.setCode(tradeInfo.getBigKind());
			TradeKind tradeKind = tradeKindService.selectbycode(k);
			if(tradeKind!=null){
				tradeInfo.setBigKind(tradeKind.getName());
			}
		}
		return R.ok(tradeInfoService.save(tradeInfo));
	}

	/**
	 * 修改
	 *
	 * @param tradeInfo
	 * @return R
	 */
	@PutMapping
	@SysLog("修改行业信息（节能）")
	@ApiOperation(value = "修改行业信息（节能）", notes = "修改行业信息（节能）")
	@ApiImplicitParam(name = "TradeInfo", value = "行业信息（节能）对象", required = true, dataType = "json", paramType = "path")
	public R updateById(@RequestBody TradeInfo tradeInfo) {
		ScUser u= SecurityUtils.getUser();
		tradeInfo.setUserid(u.getId());
		tradeInfo.setUpdateTime(new Date());
		return R.ok(tradeInfoService.updateById(tradeInfo));
	}

	/**
	 * 删除
	 *
	 * @param tradeInfoId
	 * @return R
	 */
	@DeleteMapping("/{tradeInfoId}")
	@SysLog("删除行业信息（节能）")
	@ApiOperation(value = "删除行业信息（节能）", notes = "删除行业信息（节能）")
	@ApiImplicitParam(name = "tradeInfoId", value = "id", required = true, dataType = "int", paramType = "path")
	public R removeById(@PathVariable Integer tradeInfoId) {
		TradeInfo tradeInfo = tradeInfoService.getById(tradeInfoId);
		if(tradeInfo==null){
			return R.failed("行业信息不存在，删除失败！");
		}
		ScUser u= SecurityUtils.getUser();
		tradeInfo.setUserid(u.getId());
		tradeInfo.setUpdateTime(new Date());
		tradeInfo.setDelFlag("4");
		return R.ok(tradeInfoService.updateById(tradeInfo));
	}


	/**
	 * 行业信息（节能）大类下拉框
	 */
	@GetMapping("/select")
	@ApiOperation(value = "行业信息（节能）大类下拉框", notes = "行业信息（节能）大类下拉框")
	public R tradeInfoSelect() {
		return R.ok(tradeInfoService.tradeInfoSelect());
	}


	/**
	 * 行业信息（节能）树形
	 */
	@GetMapping("/tree")
	@ApiOperation(value = "行业信息（节能）树形", notes = "行业信息（节能）树形")
	public R tradeInfoTree() {
		return R.ok(tradeInfoService.tradeInfoTree());
	}
}
