package cn.iocoder.yudao.module.pay.service.divideinfo;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pay.controller.admin.divideinfo.vo.DivideInfoPageReqVO;
import cn.iocoder.yudao.module.pay.controller.admin.divideinfo.vo.DivideInfoSaveReqVO;
import cn.iocoder.yudao.module.pay.dal.dataobject.divideinfo.DivideInfoDO;
import cn.iocoder.yudao.module.pay.dal.mysql.divideinfo.DivideInfoMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.DIVIDE_INFO_NOT_EXISTS;

/**
 * 分账明细 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class DivideInfoServiceImpl implements DivideInfoService {

    @Resource
    private DivideInfoMapper divideInfoMapper;

    @Override
    public Long createDivideInfo(DivideInfoSaveReqVO createReqVO) {
        // 插入
        DivideInfoDO divideInfo = BeanUtils.toBean(createReqVO, DivideInfoDO.class);
        divideInfoMapper.insert(divideInfo);
        // 返回
        return divideInfo.getId();
    }

    @Override
    public void updateDivideInfo(DivideInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateDivideInfoExists(updateReqVO.getId());
        // 更新
        DivideInfoDO updateObj = BeanUtils.toBean(updateReqVO, DivideInfoDO.class);
        divideInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteDivideInfo(Long id) {
        // 校验存在
        validateDivideInfoExists(id);
        // 删除
        divideInfoMapper.deleteById(id);
    }

    private void validateDivideInfoExists(Long id) {
        if (divideInfoMapper.selectById(id) == null) {
            throw exception(DIVIDE_INFO_NOT_EXISTS);
        }
    }

    @Override
    public DivideInfoDO getDivideInfo(Long id) {
        return divideInfoMapper.selectById(id);
    }

    @Override
    public PageResult<DivideInfoDO> getDivideInfoPage(DivideInfoPageReqVO pageReqVO) {
        PageResult<DivideInfoDO> divideInfoDOPageResult = divideInfoMapper.selectPage(pageReqVO);
        List<DivideInfoDO> list = divideInfoDOPageResult.getList();
        for (DivideInfoDO divideInfoDO:list){
            if(StringUtils.isNotBlank(divideInfoDO.getDFinishTime())){
                // 解析时间字符串
                ZonedDateTime zonedDateTime = ZonedDateTime.parse(divideInfoDO.getDFinishTime());
                // 定义所需的时间格式
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                // 格式化时间字符串
                divideInfoDO.setDFinishTime(zonedDateTime.format(formatter));
            }

        }
        return divideInfoDOPageResult;
    }

    @Override
    public void insertList(List<DivideInfoDO> list) {
        divideInfoMapper.insertBatch(list);
    }

    @Override
    public DivideInfoDO getDivideInfo(String outOrderNo, String account) {
        DivideInfoDO divideInfoDO = divideInfoMapper.selectOne(Wrappers.<DivideInfoDO>lambdaQuery()
                .eq(DivideInfoDO::getAccount, account)
                .eq(DivideInfoDO::getOutOrderNo, outOrderNo).last("limit 1"));
        return divideInfoDO;
    }

    @Override
    public void updateList(List<DivideInfoDO> list) {
        divideInfoMapper.updateBatch(list);
    }

    @Override
    public List<DivideInfoDO> getDivideList(String outOrderNo) {
        return divideInfoMapper.selectList(Wrappers.<DivideInfoDO>lambdaQuery()
                .eq(DivideInfoDO::getOutOrderNo, outOrderNo));
    }

    @Override
    public List<DivideInfoDO> getDivideListByTi(String transactionId) {
        return divideInfoMapper.selectList(Wrappers.<DivideInfoDO>lambdaQuery()
                .eq(DivideInfoDO::getTransactionId, transactionId));
    }
}