package com.yifu.cloud.plus.v1.yifu.insurances.service.insurance.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.insurances.entity.TInsurancePolicy;
import com.yifu.cloud.plus.v1.yifu.insurances.entity.TInsuranceTypeRate;
import com.yifu.cloud.plus.v1.yifu.insurances.mapper.insurances.TInsurancePolicyMapper;
import com.yifu.cloud.plus.v1.yifu.insurances.service.insurance.TInsurancePolicyService;
import com.yifu.cloud.plus.v1.yifu.insurances.util.BeanCopyUtils;
import com.yifu.cloud.plus.v1.yifu.insurances.vo.InsurancePolicyListEditParam;
import com.yifu.cloud.plus.v1.yifu.insurances.vo.InsurancePolicyListParam;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;


/**
 * @author licancan
 * @description 针对表【t_insurance_policy(保单号对应起止时间表)】的数据库操作Service实现
 * @date 2022-12-07 16:40:16
 */
@Service
public class TInsurancePolicyServiceImpl extends ServiceImpl<TInsurancePolicyMapper, TInsurancePolicy> implements TInsurancePolicyService {

	/**
	 * 保单号对应起止保时间分页查询
	 *
	 * @param page            分页对象
	 * @param param 查询参数
	 * @return {@link IPage <TInsurancePolicy>}
	 * @author licancan
	 */
	@Override
	public IPage<TInsurancePolicy> pageDiy(Page page, InsurancePolicyListParam param) {
		IPage<TInsurancePolicy> list = this.baseMapper.getInsurancePolicyListPage(page, param);
		return list;
	}

	/**
	 * 保单号对应起止保时间列表（不分页）
	 *
	 * @param param
	 * @return {@link List<TInsurancePolicy>}
	 * @author licancan
	 */
	@Override
	public List<TInsurancePolicy> getInsurancePolicyList(InsurancePolicyListParam param) {
		List<TInsurancePolicy> list = this.baseMapper.getInsurancePolicyList(param);
		return list;
	}

	/**
	 * 修改保单号对应起止保时间
	 *
	 * @param param
	 * @return {@link R}
	 * @author licancan
	 */
	@Override
	@Transactional(value = "insurancesTransactionManager" ,rollbackFor = {Exception.class})
	public R updateInsurancePolicy(InsurancePolicyListEditParam param) {
		TInsurancePolicy tInsurancePolicy = this.baseMapper.selectById(param.getId());
		if (!Optional.ofNullable(tInsurancePolicy).isPresent()){
			return R.failed("数据不存在");
		}
		BeanCopyUtils.copyProperties(param,tInsurancePolicy);
		updateById(tInsurancePolicy);
		//todo 操作日志
		return R.ok();
	}

	/**
	 * 删除保单号对应起止保时间
	 *
	 * @param id 主键
	 * @return {@link R}
	 * @author licancan
	 */
	@Override
	public R deleteInsurancePolicy(String id) {
		if(Common.isEmpty(id)){
			return R.failed("id不能为空");
		}
		TInsurancePolicy byId = getById(id);
		if (!Optional.ofNullable(byId).isPresent()){
			return R.failed("数据不存在");
		}
		//todo 检验保单号是否被使用，被使用不能删除
		LambdaUpdateWrapper<TInsurancePolicy> updateWrapper = new LambdaUpdateWrapper<>();
		updateWrapper.eq(TInsurancePolicy::getId,id)
				.set(TInsurancePolicy::getDeleteFlag, CommonConstants.ONE_INT);
		update(updateWrapper);
		return R.ok();
	}
}




