package com.gxwebsoft.enterprise.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.enterprise.entity.Enterprise;
import com.gxwebsoft.enterprise.service.EnterpriseService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 
 * @author GIIT-YC
 *
 */
@Tag(name = "企业信息管理")
@RestController
@RequestMapping("/api/enterprise/enterprise")
public class EnterpriseController extends BaseController {
  
	@Resource
	private EnterpriseService enterpriseService;

//	@PreAuthorize("hasAuthority('enterprise:enterprise:list')")
	@Operation(summary = "分页查询企业信息")
	@GetMapping("/page")
	public ApiResult<PageResult<Enterprise>> page(Enterprise enterprise) {
		LambdaQueryWrapper<Enterprise> wrapper = new LambdaQueryWrapper<>();
		wrapper.like(StrUtil.isNotBlank(enterprise.getName()), Enterprise::getName, enterprise.getName());
		wrapper.like(StrUtil.isNotBlank(enterprise.getCreditCode()), Enterprise::getCreditCode, enterprise.getCreditCode());
		wrapper.orderByAsc(Enterprise::getName);
		
		final Page<Enterprise> page = new Page<>(enterprise.getPage(), enterprise.getLimit());
		final IPage<Enterprise> p = enterpriseService.page(page, wrapper);
		
		return success(new PageResult<Enterprise>(p.getRecords(), p.getTotal()));
	}
	
//	@PreAuthorize("hasAuthority('enterprise:enterprise:list')")
	@Operation(summary = "查询全部企业信息")
	@GetMapping("/list")
	public ApiResult<List<Enterprise>> list(Enterprise enterprise) {
		LambdaQueryWrapper<Enterprise> wrapper = new LambdaQueryWrapper<>();
		wrapper.like(StrUtil.isNotBlank(enterprise.getName()), Enterprise::getName, enterprise.getName());
		wrapper.like(StrUtil.isNotBlank(enterprise.getCreditCode()), Enterprise::getCreditCode, enterprise.getCreditCode());
		return success(enterpriseService.list(wrapper));
	}

//	@PreAuthorize("hasAuthority('enterprise:enterprise:list')")
	@Operation(summary = "根据id查询企业信息")
	@GetMapping("/{id}")
	public ApiResult<Enterprise> get(@PathVariable("id") Integer id) {
		return success(enterpriseService.getById(id));
	}

//	@PreAuthorize("hasAuthority('enterprise:enterprise:list')")
	@Operation(summary = "根据CreditCode查询企业信息")
	@GetMapping("/creditCode/{creditCode}")
	public ApiResult<Enterprise> getByCreditCode(@PathVariable("creditCode") String creditCode) {
		Enterprise enterprise = enterpriseService.getOne(new LambdaQueryWrapper<Enterprise>().eq(Enterprise::getCreditCode, creditCode));
		return success(enterprise);
	}

//	@PreAuthorize("hasAuthority('enterprise:enterprise:save')")
	@OperationLog
	@Operation(summary = "添加企业信息")
	@PostMapping()
	public ApiResult<?> save(@RequestBody Enterprise enterprise) {
		// 记录当前登录用户id
		User loginUser = getLoginUser();
		if (loginUser != null) {
			enterprise.setUserId(loginUser.getUserId());
			final Enterprise one = enterpriseService.getOne(new LambdaQueryWrapper<Enterprise>().eq(Enterprise::getCreditCode, enterprise.getCreditCode()));
			if (!ObjectUtil.isEmpty(one)) {
				return fail("企业统一信用代码已存在");
			}
			if (enterpriseService.save(enterprise)) {
				//TODO 查询知识库(kb_name=enterprise.getCreditCode)
				
				//TODO 新建知识库
				String kbId = "pggi9mpair";
				
				//绑定知识库
				enterprise.setKbId(kbId);
				enterpriseService.updateById(enterprise);
				return success("添加成功");
			}
		}
		return fail("添加失败");
	}

//	@PreAuthorize("hasAuthority('enterprise:enterprise:update')")
	@OperationLog
	@Operation(summary = "修改企业信息")
	@PutMapping()
	public ApiResult<?> update(@RequestBody Enterprise enterprise) {
		if(StrUtil.isEmpty(enterprise.getKbId())) {
			//TODO 查询知识库
			
			//TODO 新建知识库
			String kbId = "pggi9mpair";
			
			//绑定知识库
			enterprise.setKbId(kbId);
		}
		if (enterpriseService.updateById(enterprise)) {
			return success("修改成功");
		}
		return fail("修改失败");
	}

//	@PreAuthorize("hasAuthority('enterprise:enterprise:remove')")
	@OperationLog
	@Operation(summary = "删除企业信息")
	@DeleteMapping("/{id}")
	public ApiResult<?> remove(@PathVariable("id") Integer id) {
		if (enterpriseService.removeById(id)) {
			return success("删除成功");
		}
		return fail("删除失败");
	}

//	@PreAuthorize("hasAuthority('enterprise:enterprise:remove')")
	@OperationLog
	@Operation(summary = "批量删除企业信息")
	@DeleteMapping("/batch")
	public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
		if (enterpriseService.removeByIds(ids)) {
			return success("删除成功");
		}
		return fail("删除失败");
	}

}
