/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.guosen.bussinessRegisterRequest.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.common.constant.SystemConstant;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.guosen.bussiness.entity.Bussiness;
import org.springblade.modules.guosen.bussiness.service.IBussinessService;
import org.springblade.modules.guosen.bussinessContactInfo.entity.BussinessContactInfo;
import org.springblade.modules.guosen.bussinessContactInfo.service.IBussinessContactInfoService;
import org.springblade.modules.guosen.bussinessInfo.entity.BussinessInfo;
import org.springblade.modules.guosen.bussinessInfo.service.IBussinessInfoService;
import org.springblade.modules.guosen.bussinessRegisterRequest.dto.BussinessRegisterProductInfoDTO;
import org.springblade.modules.guosen.bussinessRegisterRequest.dto.UserRegisterRequestDTO;
import org.springblade.modules.guosen.bussinessuserinfo.entity.BussinessUserInfo;
import org.springblade.modules.guosen.bussinessuserinfo.service.IBussinessUserInfoService;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.guosen.bussinessRegisterRequest.entity.BussinessRegisterRequest;
import org.springblade.modules.guosen.bussinessRegisterRequest.vo.BussinessRegisterRequestVO;
import org.springblade.modules.guosen.bussinessRegisterRequest.wrapper.BussinessRegisterRequestWrapper;
import org.springblade.modules.guosen.bussinessRegisterRequest.service.IBussinessRegisterRequestService;
import org.springblade.core.boot.ctrl.BladeController;

/**
 *  控制器
 *
 * @author BladeX
 * @since 2021-09-22
 */
@RestController
@AllArgsConstructor
@RequestMapping("cyzh-bussiness-register-request/bussinessregisterrequest")
@Api(value = "", tags = "接口")
public class BussinessRegisterRequestController extends BladeController {

	private final IBussinessRegisterRequestService bussinessRegisterRequestService;
	private final IBussinessInfoService bussinessInfoService;
	private final IBussinessContactInfoService bussinessContactInfoService;
	private final IBussinessService bussinessService;
	private final IBussinessUserInfoService userInfoService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入bussinessRegisterRequest")
	public R<BussinessRegisterRequestVO> detail(BussinessRegisterRequest bussinessRegisterRequest) {
		BussinessRegisterRequest detail = bussinessRegisterRequestService.getOne(Condition.getQueryWrapper(bussinessRegisterRequest));
		return R.data(BussinessRegisterRequestWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入bussinessRegisterRequest")
	public R<IPage<BussinessRegisterRequestVO>> list(BussinessRegisterRequest bussinessRegisterRequest, Query query) {
		IPage<BussinessRegisterRequest> pages = bussinessRegisterRequestService.page(Condition.getPage(query), Condition.getQueryWrapper(bussinessRegisterRequest));
		return R.data(BussinessRegisterRequestWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入bussinessRegisterRequest")
	public R<IPage<BussinessRegisterRequestVO>> page(BussinessRegisterRequestVO bussinessRegisterRequest, Query query) {
		IPage<BussinessRegisterRequestVO> pages = bussinessRegisterRequestService.selectBussinessRegisterRequestPage(Condition.getPage(query), bussinessRegisterRequest);
		return R.data(pages);
	}

	/**
	 * 新增 
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入bussinessRegisterRequest")
	public R save(@Valid @RequestBody BussinessRegisterRequest bussinessRegisterRequest) {
		return R.fail("不允许新增");
	}

	/**
	 * 修改 
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入bussinessRegisterRequest")
	public R update(@Valid @RequestBody BussinessRegisterRequest bussinessRegisterRequest) {
		return R.fail("不允许修改");
	}

	/**
	 * 入驻商进件
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "入驻商进件", notes = "传入bussinessRegisterRequest")
	public R submit(@Valid @RequestBody BussinessRegisterRequest bussinessRegisterRequest) {
		BladeUser user = AuthUtil.getUser();
		Bussiness bussiness = bussinessService.getBaseMapper().selectOne(new QueryWrapper<>(new Bussiness()).lambda()
				.eq(Bussiness::getAccountId, user.getUserId()));
		Assert.notNull(bussiness, "当前账号为非采购商");
		BussinessInfo dbInfo = bussinessInfoService.getById(bussinessRegisterRequest.getBussinessInfoId());
		Assert.notNull(dbInfo, "商户主体信息不存在");
		Integer registerStatus = dbInfo.getRegisterStatus();
		if(registerStatus.intValue() == SystemConstant.BussinessRegisterStatus.SUCCESS.getValue()){
			return R.fail("商户已进件审核成功，请勿修改");
		}
		if(registerStatus.intValue() == SystemConstant.BussinessRegisterStatus.WAIT_AUDIT.getValue() ||registerStatus.intValue() == SystemConstant.BussinessRegisterStatus.SUBMIT_SUCCESS.getValue()) {
			return R.fail("商户进件审核中，请勿修改");
		}
		if(registerStatus.intValue() == SystemConstant.BussinessRegisterStatus.WAIT_SIGNING.getValue()){
			return R.fail("商户待签署协议，请前往签署协议");
		}
		BussinessContactInfo dbLegal = bussinessContactInfoService.getByBussinessInfoId(bussinessRegisterRequest.getBussinessInfoId(), SystemConstant.BussinessContactType.LEGAL.getValue());
		Assert.notNull(dbLegal, "商户法人信息不存在，请先完善法人信息");
		BussinessContactInfo dbContact = bussinessContactInfoService.getByBussinessInfoId(bussinessRegisterRequest.getBussinessInfoId(), SystemConstant.BussinessContactType.CONTACT.getValue());
		Assert.notNull(dbContact, "商户联系人信息不存在，请先完善联系人信息");
		R merchantRegister = bussinessRegisterRequestService.merchantRegister(bussiness, dbInfo, dbLegal, dbContact);
		return merchantRegister;
	}
	
	/**
	 * 删除 
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(bussinessRegisterRequestService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 产品费率变更
	 */
	@PostMapping("/productFeeModify")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "产品费率变更", notes = "传入bussinessRegisterProductInfoDTO")
	public R productFeeModify(@RequestBody BussinessRegisterProductInfoDTO bussinessRegisterProductInfoDTO) {
		Bussiness bussiness = this.bussinessService.getById(bussinessRegisterProductInfoDTO.getBussinessId());
		Assert.notNull(bussiness, "商户不存在");
		if(bussiness.getRegisterStatus() == null || bussiness.getRegisterStatus().intValue() != SystemConstant.BussinessRegisterStatus.SUCCESS.getValue()){
			return R.fail("商户未完成进件，无法变更产品费率");
		}
		BussinessInfo bussinessInfo = this.bussinessInfoService.getByBussinessId(bussiness.getId());
		Assert.notNull(bussinessInfo, "商户进件记录不存在");
		return this.bussinessRegisterRequestService.productFeeModify(bussinessInfo, bussinessRegisterProductInfoDTO);
	}

	/**
	 * 小微用户进件
	 */
	@PostMapping("/userRegister")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "小微进件", notes = "传入bussinessRegisterRequest")
	public R userRegister(@Valid @RequestBody UserRegisterRequestDTO userRegisterRequestDTO) {
		BussinessUserInfo userInfo = userInfoService.getById(userRegisterRequestDTO.getUserInfoId());
		Assert.notNull(userInfo, "用户信息不存在");
		Integer registerStatus = userInfo.getRegisterStatus();
		if(registerStatus.intValue() == SystemConstant.BussinessRegisterStatus.SUCCESS.getValue()){
			return R.fail("用户已进件审核成功，请勿修改");
		}
		if(registerStatus.intValue() == SystemConstant.BussinessRegisterStatus.WAIT_AUDIT.getValue() ||registerStatus.intValue() == SystemConstant.BussinessRegisterStatus.SUBMIT_SUCCESS.getValue()) {
			return R.fail("用户进件审核中，请勿修改");
		}
		BussinessRegisterRequest registerRequest = new BussinessRegisterRequest();
		registerRequest.setUserInfoId(userInfo.getId());
		boolean requestSave = this.bussinessRegisterRequestService.save(registerRequest);
		if(requestSave){
			return this.bussinessRegisterRequestService.userRegister(registerRequest.getId(), registerRequest.getUserInfoId());
		}else{
			return R.fail("提交申请失败");
		}
	}

	/**
	 * 体验入驻商进件，仅用于体验版使用
	 */
	@PostMapping("/testSubmit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "入驻商进件", notes = "传入bussinessRegisterRequest")
	public R testSubmit(@Valid @RequestBody BussinessRegisterRequest bussinessRegisterRequest) {
		BladeUser user = AuthUtil.getUser();
		Bussiness bussiness = bussinessService.getBaseMapper().selectOne(new QueryWrapper<>(new Bussiness()).lambda()
				.eq(Bussiness::getAccountId, user.getUserId()));
		Assert.notNull(bussiness, "当前账号为非采购商");
		BussinessInfo dbInfo = bussinessInfoService.getById(bussinessRegisterRequest.getBussinessInfoId());
		Assert.notNull(dbInfo, "商户主体信息不存在");
		Integer registerStatus = dbInfo.getRegisterStatus();
		if(registerStatus.intValue() == SystemConstant.BussinessRegisterStatus.SUCCESS.getValue()){
			return R.fail("商户已进件审核成功，请勿修改");
		}
		if(registerStatus.intValue() == SystemConstant.BussinessRegisterStatus.WAIT_AUDIT.getValue() ||registerStatus.intValue() == SystemConstant.BussinessRegisterStatus.SUBMIT_SUCCESS.getValue()) {
			return R.fail("商户进件审核中，请勿修改");
		}
		if(registerStatus.intValue() == SystemConstant.BussinessRegisterStatus.WAIT_SIGNING.getValue()){
			return R.fail("商户待签署协议，请前往签署协议");
		}
		BussinessContactInfo dbLegal = bussinessContactInfoService.getByBussinessInfoId(bussinessRegisterRequest.getBussinessInfoId(), SystemConstant.BussinessContactType.LEGAL.getValue());
		Assert.notNull(dbLegal, "商户法人信息不存在，请先完善法人信息");
		BussinessContactInfo dbContact = bussinessContactInfoService.getByBussinessInfoId(bussinessRegisterRequest.getBussinessInfoId(), SystemConstant.BussinessContactType.CONTACT.getValue());
		Assert.notNull(dbContact, "商户联系人信息不存在，请先完善联系人信息");
		R merchantRegister = bussinessRegisterRequestService.testMerchantRegister(bussiness, dbInfo, dbLegal, dbContact);
		return merchantRegister;
	}
}
