package com.nantian.cloud.jljc.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.entity.EnterpriseInfo;
import com.nantian.cloud.jljc.service.EnterpriseInfoService;
import com.nantian.cloud.jljc.vo.Query;
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.annotation.Inner;
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("/enterpriseInfo")
@Api(value = "enterpriseInfo",tags ="企业基本信息")
public class EnterpriseInfoController {
	private final EnterpriseInfoService enterpriseInfoService;


	/**
	 * 分页查询
	 *
	 * @param page      分页对象
	 * @param enterpriseInfo
	 * @return
	 */
	@GetMapping("/page")
	@SysLog("分页获取企业基本信息")
	@ApiOperation(value = "分页获取企业基本信息", notes = "分页获取企业基本信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
			@ApiImplicitParam(name = "enterpriseInfo", value = "企业基本信息对象", required = false, dataType = "json", paramType = "path")
	})
	public R getEnterpriseInfoPage(Page page, EnterpriseInfo enterpriseInfo) {
		enterpriseInfo.setDelFlag("1");  //筛选未删除项
		return R.ok(enterpriseInfoService.page(page, Wrappers.query(enterpriseInfo)));
	}


	@GetMapping("/getListBylevel")
	@SysLog("分页获取企业耗能级别信息")
	@ApiOperation(value = "分页获取企业耗能级别信息", notes = "分页获取企业耗能级别信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
			@ApiImplicitParam(name = "enterpriseInfo", value = "企业基本信息对象（查询条件：企业名称name、企业编码code、地区area、企业类别type、能耗级别item）",
					required = false, dataType = "json", paramType = "path")
	})
	public R getListBylevel(Page page, Query query) {
		if (query.getArea() != null && !"".equals(query.getArea())) {
			if (query.getArea().contains("00")) {
				query.setArea(StrUtil.subWithLength(query.getArea(), 0, 4)); //截取前4位
			} else if (query.getArea().contains("0000")) {
				query.setArea(StrUtil.subWithLength(query.getArea(), 0, 2)); //截取前2位
			}
		}
		return R.ok(enterpriseInfoService.getListBylevel(page, query));
	}


	/**
	 * 获取所有企业信息
	 */
	@GetMapping("/list")
	@SysLog("获取所有企业信息")
	@ApiOperation(value = "获取所有企业信息", notes = "获取所有企业信息")
	public R list() {
		return R.ok(enterpriseInfoService.list());
	}


	/**
	 * 查询企业列表（数据权限）
	 */
	@GetMapping("/queryList")
	@SysLog("查询企业列表（数据权限）")
	@ApiOperation(value = "查询企业列表（数据权限）", notes = "查询企业列表（数据权限）")
	@ApiImplicitParam(name = "Query", value = "查询条件（企业名称name、地区area、企业类别type、行业industry）", required = false, dataType = "json", paramType = "path")
	public R queryList(Query query) {
		return R.ok(enterpriseInfoService.queryList(query));
	}


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

	/**
	 * 连表查询
	 *
	 * @param enterpriseInfo
	 * @return R
	 */

	@GetMapping("/selectall")
	@SysLog("模糊查询企业基本信息")
	@ApiOperation(value = "模糊查询企业基本信息", notes = "获取企业基本信息")
	@ApiImplicitParam(name = "enterpriseInfo", value = "企业基本信息", required = false, dataType = "json", paramType = "path")
	public IPage<EnterpriseInfo> selectall(Page page,EnterpriseInfo enterpriseInfo) {
		return enterpriseInfoService.selectEnterpriseTradeType(page,enterpriseInfo);
	}

	/**
	 * 保存
	 *
	 * @param enterpriseInfo
	 * @return R
	 */
	@PostMapping
	@SysLog("新增企业基本信息")
	@ApiOperation(value = "新增企业基本信息", notes = "新增企业基本信息")
	@ApiImplicitParam(name = "enterpriseInfo", value = "企业基本信息对象", required = true, dataType = "json", paramType = "path")
	public R save(@RequestBody EnterpriseInfo enterpriseInfo) {

		enterpriseInfo.setUpdateTime(new Date());
		enterpriseInfo.setDelFlag("1");
		ScUser u = SecurityUtils.getUser();
		enterpriseInfo.setUserid(u.getId());
		if(enterpriseInfo.getRegionCode()!=null){
			enterpriseInfo.setDeptId(Integer.parseInt(enterpriseInfo.getRegionCode()));
		}else{
			enterpriseInfo.setDeptId(u.getDeptId());
			enterpriseInfo.setRegionCode(u.getDeptId().toString());
		}
		return R.ok(enterpriseInfoService.save(enterpriseInfo));
	}

	/**
	 * 修改
	 *
	 * @param enterpriseInfo
	 * @return R
	 */
	@PutMapping
	@SysLog("修改企业基本信息")
	@ApiOperation(value = "修改企业基本信息", notes = "修改企业基本信息")
	@ApiImplicitParam(name = "enterpriseInfo", value = "企业基本信息对象", required = true, dataType = "json", paramType = "path")
	public R updateById(@RequestBody EnterpriseInfo enterpriseInfo) {
		return R.ok(enterpriseInfoService.updateById(enterpriseInfo));
	}

	/**
	 * 删除
	 *
	 * @param enterpriseInfoId
	 * @return R
	 */
	@DeleteMapping("/{enterpriseInfoId}")
	@SysLog("删除企业基本信息")
	@ApiOperation(value = "删除企业基本信息", notes = "删除企业基本信息")
	@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
	public R removeById(@PathVariable Integer enterpriseInfoId) {
		return R.ok(enterpriseInfoService.removeById(enterpriseInfoId));
	}

	@GetMapping("/getEnterprise/code")
	@SysLog("根据企业信用代码获取企业基本信息")
	@ApiOperation(value = "根据企业信用代码获取企业基本信息", notes = "根据企业信用代码获取企业基本信息")
	@ApiImplicitParam(name = "企业信用代码", value = "enterpriseCode", required = false, paramType = "path")
	public R getByEnterpriseCode(@RequestParam("enterpriseCode") String enterpriseCode) {
		EnterpriseInfo enterpriseInfo=new EnterpriseInfo();
		enterpriseInfo.setEnterpriseCode(enterpriseCode);
		enterpriseInfo.setDelFlag("1");
		EnterpriseInfo info = enterpriseInfoService.getOne(Wrappers.query(enterpriseInfo));
		if(info==null){
			EnterpriseInfo e=new EnterpriseInfo();
			e.setEpc(enterpriseCode);
			e.setDelFlag("1");
			//return R.ok(enterpriseInfoService.getOne(Wrappers.query(e)));
			List<EnterpriseInfo> list = enterpriseInfoService.list(Wrappers.query(e));
			return R.ok(list.size() > 0 ? list.get(list.size() - 1) : null);
		}else{
			return R.ok(info);
		}
	}

	@Inner(value = false) //取消身份验证
	@GetMapping("/getEnterprise_wu")
	@SysLog("根据企业信用代码获取企业基本信息(无身份验证)")
	@ApiOperation(value = "根据企业信用代码获取企业基本信息", notes = "根据企业信用代码获取企业基本信息")
	@ApiImplicitParam(name = "企业信用代码", value = "enterpriseCode", required = false, paramType = "path")
	public R getByEnterpriseCode_wu(@RequestParam("enterpriseCode") String enterpriseCode) {
		EnterpriseInfo enterpriseInfo=new EnterpriseInfo();
		enterpriseInfo.setEnterpriseCode(enterpriseCode);
		enterpriseInfo.setDelFlag("1");
		EnterpriseInfo info = enterpriseInfoService.getOne(Wrappers.query(enterpriseInfo));
		if(info==null){
			EnterpriseInfo e=new EnterpriseInfo();
			e.setEpc(enterpriseCode);
			e.setDelFlag("1");
			//return R.ok(enterpriseInfoService.getOne(Wrappers.query(e)));
			List<EnterpriseInfo> list = enterpriseInfoService.list(Wrappers.query(e));
			return R.ok(list.size() > 0 ? list.get(list.size() - 1) : null);
		}else{
			return R.ok(info);
		}
	}

}
