package org.springblade.business.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.apache.commons.lang.StringUtils;
import org.springblade.business.entity.FixedFlowLink;
import org.springblade.business.service.IFixedFlowLinkService;
import org.springblade.common.utils.SnowFlakeUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.business.entity.FixedFlow;
import org.springblade.business.vo.FixedFlowVO;
import org.springblade.business.service.IFixedFlowService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.ArrayList;
import java.util.List;

/**
 *  控制器
 *
 * @author BladeX
 * @since 2022-07-01
 */
@RestController
@AllArgsConstructor
@RequestMapping("/fixedFlow")
@Api(tags = "预设流程接口")
public class FixedFlowController extends BladeController {

	private final IFixedFlowService fixedFlowService;

	private final IFixedFlowLinkService fixedFlowLinkService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "列表数据的id字段")
	public R<FixedFlowVO> detail(String id) {
		FixedFlow fixedFlow = this.fixedFlowService.getById(id);
		//转换实体
		FixedFlowVO fixedFlowVO = new FixedFlowVO();
		BeanUtils.copyProperties(fixedFlow, fixedFlowVO);

		//查询环节信息
		List<FixedFlowLink> linkList = this.fixedFlowLinkService.selectFixedFlowLink(id);
		fixedFlowVO.setFixedFlowLinkList(linkList);

		return R.data(fixedFlowVO);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入fixedFlow")
	public R<IPage<FixedFlowVO>> page(FixedFlowVO vo) {
		if(vo.getCurrent() == null || vo.getSize() == null){
			return R.data(-1, null, "缺少size或current参数");
		}
		return R.data(this.fixedFlowService.selectFixedFlowPage(vo));
	}

	/**
	 * 新增 
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "新增", notes = "传入fixedFlow")
	public R save(@Valid @RequestBody FixedFlowVO vo) {
		//获取环节处理人顺序
		String linkUserJoinString = vo.getLinkUserJoinString();
		//生成主表主键
		Long fixedFlowId = SnowFlakeUtil.getId();

		if(StringUtils.isNotEmpty(linkUserJoinString)){
			//新增环节集合
			List<FixedFlowLink> linkList = new ArrayList<>();

			String[] linkUsers = linkUserJoinString.split(",");
			int sort = 1;
			for(String linkUser : linkUsers){
				//拆分姓名及ID
				String[] links = linkUser.split("-");
				linkList.add(new FixedFlowLink(links[0], links[1], vo.getProjectId(), vo.getContractId(), fixedFlowId, sort));
				sort ++;
			}
			//保存环节
			this.fixedFlowLinkService.saveBatch(linkList);
		}
		//生成主表数据
		FixedFlow flow = new FixedFlow();
		BeanUtils.copyProperties(vo, flow);
		flow.setId(fixedFlowId);

		return R.status(this.fixedFlowService.save(flow));
	}

	/**
	 * 修改 
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "修改", notes = "传入fixedFlow对象")
	public R update(@Valid @RequestBody FixedFlowVO vo) {
		//获取环节处理人顺序
		String linkUserJoinString = vo.getLinkUserJoinString();
		if(StringUtils.isNotEmpty(linkUserJoinString)){
			//清空原本环节
			this.fixedFlowLinkService.update(Wrappers.<FixedFlowLink>lambdaUpdate().set(FixedFlowLink::getIsDeleted, 1).eq(FixedFlowLink::getFixedFlowId, vo.getId()));

			//新增环节集合
			List<FixedFlowLink> linkList = new ArrayList<>();

			String[] linkUsers = linkUserJoinString.split(",");
			int sort = 1;
			for(String linkUser : linkUsers){
				//拆分姓名及ID
				String[] links = linkUser.split("-");
				linkList.add(new FixedFlowLink(links[0], links[1], vo.getProjectId(), vo.getContractId(), vo.getId(), sort));
			}
			//保存环节
			this.fixedFlowLinkService.saveBatch(linkList);
		}

		FixedFlow flow = new FixedFlow();
		BeanUtils.copyProperties(vo, flow);

		return R.status(this.fixedFlowService.updateById(flow));
	}

	
	/**
	 * 删除 
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		if(this.fixedFlowService.deleteLogic(Func.toLongList(ids))){
			//同步删除环节
			this.fixedFlowLinkService.deletedByFixedFlowId(ids);
			return R.status(true);
		}
		return R.status(false);
	}

	
}
