package com.dk.mdm.service.mst;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dk.common.infrastructure.annotaiton.Pagination;
import com.dk.common.infrastructure.constant.Constant;
import com.dk.common.model.pojo.PageList;
import com.dk.common.response.ResponseResultUtil;
import com.dk.common.response.ResponseResultVO;
import com.dk.mdm.infrastructure.convert.mst.MoneyAccountConvert;
import com.dk.mdm.mapper.mac.MacTransferItemMapper;
import com.dk.mdm.mapper.mac.MacTransferMapper;
import com.dk.mdm.mapper.mst.MoneyAccountItemMapper;
import com.dk.mdm.model.pojo.mac.MacTransfer;
import com.dk.mdm.model.pojo.mac.MacTransferItem;
import com.dk.mdm.model.pojo.mst.MoneyAccount;
import com.dk.mdm.mapper.mst.MoneyAccountMapper;
import com.dk.common.service.BaseService;
import com.dk.common.mapper.BaseMapper;
import com.dk.mdm.model.pojo.mst.MoneyAccountItem;
import com.dk.mdm.model.query.mst.MoneyAccountQuery;
import com.dk.mdm.model.response.mst.MoneyAccountResponse;
import com.dk.mdm.model.vo.mst.MoneyAccountVO;
import com.dk.mdm.service.common.CommonService;
import com.dk.mdm.service.mac.AccountService;
import com.dk.mdm.service.mac.MacTransferItemService;
import com.dk.mdm.service.mac.MacTransferService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
@Transactional
public class MoneyAccountService extends BaseService<MoneyAccount> {

	/**
	 * @desc   : 重写主键
	 * @author : 宋扬
	 * @date   : 2023/1/9 10:39
	 */
	@Override
	public String getPrimaryKey() {
		return "mac_id";
	}

	@Override
	public BaseMapper<MoneyAccount> getRepository() {
		return moneyAccountMapper;
	}

	@Autowired
	private MacTransferService macTransferService;

	@Autowired
	private MacTransferItemService macTransferItemService;

	@Autowired
	private MoneyAccountMapper moneyAccountMapper;

	@Autowired
	private MacTransferItemMapper macTransferItemMapper;

	@Autowired
	private MoneyAccountItemService moneyAccountItemService;

	@Autowired
	private MoneyAccountItemMapper moneyAccountItemMapper;

	@Autowired
	private AccountService accountService;

	@Autowired
	private CommonService commonService;

	@Autowired
	private MacTransferMapper macTransferMapper;

	@Autowired
	private MoneyAccountConvert moneyAccountConvert;


	/**
	 * @desc   : 条件查询
	 * @author : 宋扬
	 * @date   : 2023/2/29 10:40
	 */
	@Pagination
	public ResponseResultVO<PageList<MoneyAccountResponse>> selectByCond(MoneyAccountQuery moneyAccountQuery) {
		return super.mergeListWithCount(moneyAccountQuery, moneyAccountMapper.selectByCond(moneyAccountQuery),
				moneyAccountMapper.countByCond(moneyAccountQuery));
	}

	/**
	 * @desc   : 条件查询
	 * @author : 宋扬
	 * @date   : 2023/2/29 10:40
	 */
	@Pagination
	public ResponseResultVO selectById(String id) {
		MoneyAccountResponse moneyAccountResponse = moneyAccountMapper.selectById(id);
		return ResponseResultUtil.success(moneyAccountResponse);
	}

	/**
	 * @desc   : 维修小程序列表
	 * @author : 王英杰
	 * @date   : 2023/2/29 10:40
	 */
	@Pagination
	public ResponseResultVO<Map<String, Object>> wxSelectByCond(MoneyAccountQuery moneyAccountQuery) {
		// 获取系统基础数据
		List<Map<String, Object>> list = moneyAccountMapper.wxSelectByCond(moneyAccountQuery);
		Map<String, Object> objectObjectHashMap = new HashMap<>();
		objectObjectHashMap.put("list", list);
		return ResponseResultUtil.success(objectObjectHashMap);
	}

	/**
	 * @desc : 保存方法
	 * @author : 宋扬
	 * @date : 2023/2/29 10:49
	 */
	@Transactional(
			rollbackFor = {Exception.class}
	)
	public ResponseResultVO<?> insert(MoneyAccountVO moneyAccountVO) {
		// 转化实体
		MoneyAccount moneyAccount = moneyAccountConvert.convertToPo(moneyAccountVO);
		// 赋值期初
		moneyAccount.setBalance(moneyAccountVO.getInitBalance());
		// 获取编码和主键UuId
		Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.MONEYACCOUNT.getName(),true);
		// 获取显示顺序
		Integer displayNo = commonService.getMaxDisplayNo(Constant.DisplayNoTable.MONEYACCOUNT);
		moneyAccount.setMacId(codeMap.get("outId").toString()).setMacCode(codeMap.get("outNote").toString()).setDisplayNo(displayNo);
		super.insert(moneyAccount);

		// 插入资金转账表
		MacTransfer macTransfer = new MacTransfer();
		codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.MACTRANSFER.getName(),true);
		macTransfer.setTrfId(codeMap.get("outId").toString()).setTrfNo(codeMap.get("outNote").toString())
			.setTrfType(0).setOrgId(moneyAccountVO.getOrgId()).setStaffId(moneyAccountVO.getStaffId())
			.setAccDate(moneyAccount.getAccDate()).setMakeStaff(moneyAccountVO.getMakeStaff()).setCpId(moneyAccount.getCpId());
		macTransferMapper.insert(macTransfer);

		// 插入资金转账明细表
		MacTransferItem macTransferItem = new MacTransferItem();
		macTransferItem.setTrfId(macTransfer.getTrfId()).setTrfInMac(moneyAccount.getMacId()).setAmtTrf(moneyAccount.getBalance())
				.setCpId(macTransfer.getCpId()).setMakeStaff(macTransfer.getMakeStaff());
		macTransferItemMapper.insert(macTransferItem);

		// 插入资金流水
		MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
		moneyAccountItem.setMacId(moneyAccount.getMacId()).setFlowType(Constant.FlowType.ZHUAN_ZHANG.getName())
				.setInvoiceId(macTransferItem.getItemId()).setAmtInflow(moneyAccount.getBalance()).setAccDate(moneyAccount.getAccDate())
				.setMakeStaff(moneyAccountVO.getMakeStaff()).setCpId(moneyAccount.getCpId());
		moneyAccountItemMapper.insert(moneyAccountItem);

		return ResponseResultUtil.success();
	}

	/**
	 * @desc : 编辑方法
	 * @author : 宋扬
	 * @date : 2023/2/29 10:49
	 */
	@Transactional(
			rollbackFor = {Exception.class}
	)
	public ResponseResultVO<Boolean> update(MoneyAccountVO moneyAccountVO) {
		// 转化实体
		MoneyAccount moneyAccount = moneyAccountConvert.convertToPo(moneyAccountVO);
			super.update(moneyAccount, new UpdateWrapper<MoneyAccount>().lambda().eq(MoneyAccount::getMacId,
				UUID.fromString(moneyAccount.getMacId())));

		// 赋值期初
		moneyAccount.setBalance(moneyAccountVO.getInitBalance());

		// 更新资金转账明细
		MacTransferItem macTransferItemForUpdate = macTransferItemMapper.selectByInMacIdForUpdate(moneyAccount.getMacId());
		// 有资金转账就更新，没有就新建
		if(macTransferItemForUpdate != null){
			MacTransferItem macTransferItemUpdate = new MacTransferItem();
			macTransferItemUpdate.setAmtTrf(moneyAccount.getBalance()).setItemId(macTransferItemForUpdate.getItemId());
			macTransferItemService.updateByUuid(macTransferItemUpdate);

			// 更新资金转账账务日期
			MacTransfer macTransferUpdate = new MacTransfer();
			macTransferUpdate.setAccDate(moneyAccountVO.getAccDate()).setTrfId(macTransferItemForUpdate.getTrfId());
			macTransferService.updateByUuid(macTransferUpdate);
		}else{
			// 插入资金转账表
			MacTransfer macTransfer = new MacTransfer();
			Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.MACTRANSFER.getName(),true);
			macTransfer.setTrfId(codeMap.get("outId").toString()).setTrfNo(codeMap.get("outNote").toString())
					.setTrfType(0).setOrgId(moneyAccountVO.getOrgId()).setStaffId(moneyAccountVO.getStaffId())
					.setAccDate(moneyAccount.getAccDate()).setMakeStaff(moneyAccountVO.getMakeStaff()).setCpId(moneyAccount.getCpId());
			macTransferMapper.insert(macTransfer);

			// 插入资金转账明细表
			MacTransferItem macTransferItem = new MacTransferItem();
			macTransferItem.setTrfId(macTransfer.getTrfId()).setTrfInMac(moneyAccount.getMacId()).setAmtTrf(moneyAccount.getBalance())
					.setCpId(macTransfer.getCpId()).setMakeStaff(macTransfer.getMakeStaff());
			macTransferItemMapper.insert(macTransferItem);
			macTransferItemForUpdate = macTransferItem;

			// 插入资金流水
			MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
			moneyAccountItem.setMacId(moneyAccount.getMacId()).setFlowType(Constant.FlowType.ZHUAN_ZHANG.getName())
					.setInvoiceId(macTransferItem.getItemId()).setAmtInflow(moneyAccount.getBalance()).setAccDate(moneyAccount.getAccDate())
					.setMakeStaff(moneyAccountVO.getMakeStaff()).setCpId(moneyAccount.getCpId());
			moneyAccountItemMapper.insert(moneyAccountItem);
		}

		// 更新资金流水
		List<MoneyAccountItem> moneyAccountItemList = moneyAccountItemMapper.selectByInvoiceIdForUpdate(macTransferItemForUpdate.getItemId());
		MoneyAccountItem moneyAccountItemUpdate = new MoneyAccountItem();
		moneyAccountItemUpdate.setAmtInflow(moneyAccount.getBalance()).setAccDate(moneyAccount.getAccDate()).setItemId(moneyAccountItemList.get(0).getItemId());
		moneyAccountItemService.updateByUuid(moneyAccountItemUpdate);

		// 更新账户余额
		accountService.updateMac(moneyAccount.getMacId());
		return ResponseResultUtil.success();
	}
}
