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

import cn.iocoder.yudao.module.pay.dal.dataobject.divideinfo.DivideInfoDO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import cn.iocoder.yudao.module.pay.controller.admin.wxprofitsharinginfo.vo.*;
import cn.iocoder.yudao.module.pay.dal.dataobject.wxprofitsharinginfo.WxProfitsharingInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.pay.dal.mysql.wxprofitsharinginfo.WxProfitsharingInfoMapper;

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

/**
 * 微信分账明细 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class WxProfitsharingInfoServiceImpl implements WxProfitsharingInfoService {

    @Resource
    private WxProfitsharingInfoMapper wxProfitsharingInfoMapper;

    @Override
    public Long createWxProfitsharingInfo(WxProfitsharingInfoSaveReqVO createReqVO) {
        // 插入
        WxProfitsharingInfoDO wxProfitsharingInfo = BeanUtils.toBean(createReqVO, WxProfitsharingInfoDO.class);
        wxProfitsharingInfoMapper.insert(wxProfitsharingInfo);
        // 返回
        return wxProfitsharingInfo.getId();
    }

    @Override
    public void updateWxProfitsharingInfo(WxProfitsharingInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateWxProfitsharingInfoExists(updateReqVO.getId());
        // 更新
        WxProfitsharingInfoDO updateObj = BeanUtils.toBean(updateReqVO, WxProfitsharingInfoDO.class);
        wxProfitsharingInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteWxProfitsharingInfo(Long id) {
        // 校验存在
        validateWxProfitsharingInfoExists(id);
        // 删除
        wxProfitsharingInfoMapper.deleteById(id);
    }

    private void validateWxProfitsharingInfoExists(Long id) {
        if (wxProfitsharingInfoMapper.selectById(id) == null) {
            throw exception(WX_PROFITSHARING_INFO_NOT_EXISTS);
        }
    }

    @Override
    public WxProfitsharingInfoDO getWxProfitsharingInfo(Long id) {
        return wxProfitsharingInfoMapper.selectById(id);
    }

    @Override
    public PageResult<WxProfitsharingInfoDO> getWxProfitsharingInfoPage(WxProfitsharingInfoPageReqVO pageReqVO) {
        PageResult<WxProfitsharingInfoDO> pageResult = wxProfitsharingInfoMapper.selectPage(pageReqVO);

        for (WxProfitsharingInfoDO infoDO:pageResult.getList()){
            if(StringUtils.isNotBlank(infoDO.getDCreateTime())){
                // 解析时间字符串
                ZonedDateTime zonedDateTime = ZonedDateTime.parse(infoDO.getDCreateTime());
                // 定义所需的时间格式
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                // 格式化时间字符串
                infoDO.setDCreateTime(zonedDateTime.format(formatter));
            }

        }
        return pageResult;
    }

}