/*
 *      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.bussinessContactInfo.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.bussinessInfo.entity.BussinessInfo;
import org.springblade.modules.guosen.bussinessInfo.service.IBussinessInfoService;
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.bussinessContactInfo.entity.BussinessContactInfo;
import org.springblade.modules.guosen.bussinessContactInfo.vo.BussinessContactInfoVO;
import org.springblade.modules.guosen.bussinessContactInfo.wrapper.BussinessContactInfoWrapper;
import org.springblade.modules.guosen.bussinessContactInfo.service.IBussinessContactInfoService;
import org.springblade.core.boot.ctrl.BladeController;

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

	private final IBussinessContactInfoService bussinessContactInfoService;
	private final IBussinessInfoService bussinessInfoService;
	private final IBussinessService bussinessService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入bussinessContactInfo")
	public R<BussinessContactInfoVO> detail(BussinessContactInfo bussinessContactInfo) {
		BussinessContactInfo detail = bussinessContactInfoService.getOne(Condition.getQueryWrapper(bussinessContactInfo));
		return R.data(BussinessContactInfoWrapper.build().entityVO(detail));
	}

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


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

	/**
	 * 新增 
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入bussinessContactInfo")
	public R save(@Valid @RequestBody BussinessContactInfo bussinessContactInfo) {
		return R.fail("请勿新增");
//		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(bussinessContactInfo.getBussinessInfoId());
//		Assert.notNull(dbInfo, "商户主体信息不存在");
//		boolean save = bussinessContactInfoService.save(bussinessContactInfo);
//		if(!save){
//			return R.fail("保存失败");
//		}
//		return R.data(bussinessContactInfo);
	}

	/**
	 * 修改 
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入bussinessContactInfo")
	public R update(@Valid @RequestBody BussinessContactInfo bussinessContactInfo) {
		return R.fail("请勿修改");
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入bussinessContactInfo")
	public R submit(@Valid @RequestBody BussinessContactInfo bussinessContactInfo) {
		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(bussinessContactInfo.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("商户待签署协议，请前往签署协议");
		}
		boolean saveOrUpdate = bussinessContactInfoService.saveOrUpdate(bussinessContactInfo);
		if(!saveOrUpdate){
			return R.fail("保存失败");
		}
		return R.data(bussinessContactInfo);
	}

	
	/**
	 * 删除 
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(bussinessContactInfoService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 根据条件获取详情
	 */
	@GetMapping("/getByTypeAndBussinessInfoId")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入bussinessContactInfo")
	public R<BussinessContactInfo> getByTypeAndBussinessInfoId(BussinessContactInfo bussinessContactInfo) {
		BussinessContactInfo detail = bussinessContactInfoService.getOne(Condition.getQueryWrapper(bussinessContactInfo));
		if(detail != null){
			return R.data(detail);
		}else{
			return R.data(new BussinessContactInfo());
		}
	}
	
}
