package org.springblade.estate.service.impl;

import org.springblade.common.utils.GenerateNum;
import org.springblade.community.entity.Agency;
import org.springblade.community.feign.IAgencyClient;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.estate.dto.RepairDTO;
import org.springblade.estate.entity.Repair;
import org.springblade.estate.entity.WorkOrder;
import org.springblade.estate.service.IWorkOrderService;
import org.springblade.estate.vo.RepairVO;
import org.springblade.estate.mapper.RepairMapper;
import org.springblade.estate.service.IRepairService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.system.expand.feign.IConfClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

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

/**
 * 报事工单 服务实现类
 *
 * @author BladeX
 * @since 2021-03-30
 */
@Service
public class RepairServiceImpl extends BaseServiceImpl<RepairMapper, Repair> implements IRepairService {

	@Autowired
	private IAgencyClient agencyClient;
	@Autowired
	private IConfClient confClient;
	@Autowired
	private IWorkOrderService workOrderService;

	@Override
	public IPage<RepairVO> selectRepairPage(IPage<RepairVO> page, RepairVO repair) {
		return page.setRecords(baseMapper.selectRepairPage(page, repair));
	}

	@Override
	public R repairSubmit(RepairDTO repairDTO) {
		Long agencyId = repairDTO.getAgencyId();
		Agency agency = agencyClient.getById(agencyId);
		repairDTO.setTenantId(agency.getTenantId());
		repairDTO.setRepairNo(GenerateNum.generateNum());
		if(StringUtil.isNotBlank(repairDTO.getPic())){
			List<String> picList = Func.toStrList(repairDTO.getPic());
			List<String> picPathList = new ArrayList<>();
			for(String pic : picList){
				picPathList.add(confClient.removePrefix(pic));
			}
			repairDTO.setPic(Func.join(picPathList, ","));
		}
		return R.status(this.saveOrUpdate(repairDTO));
	}

	@Override
	public R generateWorkOrder(RepairDTO repairDTO) {
		Repair repair = this.getById(repairDTO.getId());
		if(repair == null){
			return R.fail("事件不存在");
		}
		if(repair.getHasWorkOrder() == 1){
			return R.fail("工单已生成，请勿重复");
		}
		R<WorkOrder> result = this.workOrderService.generateWorkOrder(repairDTO.getWorkOrderName(), 2,
			repair.getReportDetail(), repairDTO.getBizTag(), repairDTO.getGroupUserId(),
			repair.getPic(), repair.getReportor(), repair.getReportorPhone(), repairDTO.getHandleTime(), repairDTO.getGroupId());
		if(result.isSuccess()){
			repair.setHandler(repairDTO.getHandler());
			repair.setHandlerPhone(repairDTO.getHandlerPhone());
			repair.setHandleTime(repairDTO.getHandleTime());
			repair.setWorkOrderGroupId(repairDTO.getGroupId());
			repair.setWorkOrderGroupUserId(repairDTO.getGroupUserId());
			repair.setAcceptStatus(1);
			repair.setAcceptReason("已受理");
			repair.setHasWorkOrder(1);
			repair.setWorkOrderId(result.getData().getId());
			repair.setHandleStatus(0);
			return R.status(this.updateById(repair));
		}else{
			return R.fail("工单生成失败");
		}

	}

	@Override
	public R toHandle(RepairDTO repairDTO) {
		Repair repair = this.getById(repairDTO.getId());
		if(repair == null){
			return R.fail("事件不存在");
		}
		repair.setHandleStatus(repairDTO.getHandleStatus());
		return R.status(this.updateById(repair));
	}

}
