/**
 * BladeX Commercial License Agreement
 * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
 * <p>
 * Use of this software is governed by the Commercial License Agreement
 * obtained after purchasing a license from BladeX.
 * <p>
 * 1. This software is for development use only under a valid license
 * from BladeX.
 * <p>
 * 2. Redistribution of this software's source code to any third party
 * without a commercial license is strictly prohibited.
 * <p>
 * 3. Licensees may copyright their own code but cannot use segments
 * from this software for such purposes. Copyright of this software
 * remains with BladeX.
 * <p>
 * Using this software signifies agreement to this License, and the software
 * must not be used for illegal purposes.
 * <p>
 * Author: Chill Zhuang (bladejava@qq.com)
 */
package org.springblade.transport.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.transport.excel.CustomerArchiveExcel;
import org.springblade.transport.pojo.entity.CustomerArchive;
import org.springblade.transport.pojo.vo.CustomerArchiveVO;
import org.springblade.transport.pojo.vo.CustomerCreditScoreVO;
import org.springblade.transport.service.ICustomerArchiveService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 客商档案 控制器
 *
 * @author Chill
 */
@RestController
@AllArgsConstructor
@PreAuth(menu = "customer_archive")
@RequestMapping("/customer-archive")
@Tag(name = "客商档案", description = "客商档案")
public class CustomerArchiveController extends BladeController {

	private final ICustomerArchiveService customerArchiveService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description = "传入id")
	public R<CustomerArchiveVO> detail(@Parameter(description = "主键", required = true) @RequestParam Long id) {
		return R.data(customerArchiveService.detail(id));
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description = "传入customerArchive")
	public R<IPage<CustomerArchiveVO>> list(CustomerArchiveVO customerArchive, Query query) {
		IPage<CustomerArchiveVO> pages = customerArchiveService.selectCustomerArchivePage(Condition.getPage(query), customerArchive);
		return R.data(pages);
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "新增或修改", description = "传入customerArchive")
	public R submit(@RequestBody CustomerArchiveVO customerArchive) {
		boolean result = customerArchiveService.submit(customerArchive);
		return result ? R.data(customerArchive.getId()) : R.status(false);
	}

	/**
	 * 提交审核
	 */
	@PostMapping("/submit-approval")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "提交审核", description = "传入id")
	public R submitApproval(@Parameter(description = "主键", required = true) @RequestParam Long id) {
		return R.status(customerArchiveService.submitApproval(id));
	}

	/**
	 * 撤回审批
	 */
	@PostMapping("/withdraw-approval")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "撤回审批", description = "传入id")
	public R withdrawApproval(@Parameter(description = "主键", required = true) @RequestParam Long id) {
		return R.status(customerArchiveService.withdrawApproval(id));
	}

	/**
	 * 审核通过
	 */
	@PostMapping("/approve")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "审核通过", description = "传入id")
	public R approve(@Parameter(description = "主键", required = true) @RequestParam Long id) {
		return R.status(customerArchiveService.approve(id));
	}

	/**
	 * 审核不通过
	 */
	@PostMapping("/reject")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "审核不通过", description = "传入id")
	public R reject(@Parameter(description = "主键", required = true) @RequestParam Long id) {
		return R.status(customerArchiveService.reject(id));
	}

	/**
	 * 启用或停用
	 */
	@PostMapping("/status")
	@ApiOperationSupport(order = 8)
	@Operation(summary = "启用或停用", description = "传入id和status")
	public R status(@Parameter(description = "主键", required = true) @RequestParam Long id,
					@Parameter(description = "状态", required = true) @RequestParam Integer status) {
		return R.status(customerArchiveService.changeStatus(id, status));
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "逻辑删除", description = "传入ids")
	public R remove(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.status(customerArchiveService.removeDraft(ids));
	}

	/**
	 * 评分明细模板
	 */
	@GetMapping("/score-template")
	@ApiOperationSupport(order = 10)
	@Operation(summary = "评分明细模板", description = "传入评分量化表ID")
	public R<CustomerCreditScoreVO> scoreTemplate(@RequestParam(required = false) Long quantificationId) {
		return R.data(customerArchiveService.buildScoreTemplate(quantificationId));
	}

	/**
	 * 导出客商档案
	 */
	@GetMapping("/export-customer-archive")
	@ApiOperationSupport(order = 11)
	@Operation(summary = "导出客商档案")
	public void exportCustomerArchive(CustomerArchiveVO customerArchive,
									  @RequestParam(required = false) String ids,
									  HttpServletResponse response) {
		List<CustomerArchiveExcel> list = customerArchiveService.exportCustomerArchive(buildExportQuery(customerArchive, ids));
		ExcelUtil.export(response, "客商档案" + DateUtil.time(), "客商档案", list, CustomerArchiveExcel.class);
	}

	private LambdaQueryWrapper<CustomerArchive> buildExportQuery(CustomerArchiveVO customerArchive, String ids) {
		LambdaQueryWrapper<CustomerArchive> queryWrapper = Wrappers.<CustomerArchive>lambdaQuery()
			.eq(CustomerArchive::getIsDeleted, 0)
			.orderByDesc(CustomerArchive::getCreateTime);
		Long userId = AuthUtil.getUserId();
		queryWrapper.and(wrapper -> wrapper
			.exists("SELECT 1 FROM blade_user_customer_scope bucs WHERE bucs.user_id = " + userId
				+ " AND bucs.customer_id = blade_customer_archive.id"));
		if (Func.isNotEmpty(ids)) {
			queryWrapper.in(CustomerArchive::getId, Func.toLongList(ids));
		}
		if (Func.isNotEmpty(customerArchive.getCustomerCode())) {
			queryWrapper.like(CustomerArchive::getCustomerCode, customerArchive.getCustomerCode());
		}
		if (Func.isNotEmpty(customerArchive.getFullName())) {
			queryWrapper.like(CustomerArchive::getFullName, customerArchive.getFullName());
		}
		if (Func.isNotEmpty(customerArchive.getShortName())) {
			queryWrapper.like(CustomerArchive::getShortName, customerArchive.getShortName());
		}
		if (Func.isNotEmpty(customerArchive.getUnifiedCreditCode())) {
			queryWrapper.like(CustomerArchive::getUnifiedCreditCode, customerArchive.getUnifiedCreditCode());
		}
		if (Func.isNotEmpty(customerArchive.getCustomerNature())) {
			queryWrapper.eq(CustomerArchive::getCustomerNature, customerArchive.getCustomerNature());
		}
		if (Func.isNotEmpty(customerArchive.getCustomerType())) {
			queryWrapper.like(CustomerArchive::getCustomerType, customerArchive.getCustomerType());
		}
		if (Func.isNotEmpty(customerArchive.getAccessType())) {
			queryWrapper.eq(CustomerArchive::getAccessType, customerArchive.getAccessType());
		}
		if (Func.isNotEmpty(customerArchive.getApprovalStatus())) {
			queryWrapper.eq(CustomerArchive::getApprovalStatus, customerArchive.getApprovalStatus());
		}
		if (Func.isNotEmpty(customerArchive.getStatus())) {
			queryWrapper.eq(CustomerArchive::getStatus, customerArchive.getStatus());
		}
		if (Func.isNotEmpty(customerArchive.getDeptName())) {
			queryWrapper.like(CustomerArchive::getDeptName, customerArchive.getDeptName());
		}
		if (Func.isNotEmpty(customerArchive.getCreateTimeStart())) {
			queryWrapper.ge(CustomerArchive::getCreateTime, customerArchive.getCreateTimeStart());
		}
		if (Func.isNotEmpty(customerArchive.getCreateTimeEnd())) {
			queryWrapper.le(CustomerArchive::getCreateTime, customerArchive.getCreateTimeEnd());
		}
		return queryWrapper;
	}

}
