package cn.newfeifan.mall.module.distri.service.applicationforwithdrawal;

import cn.newfeifan.mall.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.newfeifan.mall.module.distri.constant.DistriConstants;
import cn.newfeifan.mall.module.distri.controller.admin.applicationforwithdrawalchannel.vo.ApplicationForWithdrawalChannelExcelRespVO;
import cn.newfeifan.mall.module.distri.controller.admin.applicationforwithdrawalchannel.vo.GZHXExcelResp;
import cn.newfeifan.mall.module.distri.enums.WithdrawalChannelTypeEnum;
import cn.newfeifan.mall.module.member.dal.dataobject.user.MemberUserDO;
import cn.newfeifan.mall.module.member.service.user.MemberUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.time.format.DateTimeFormatter;
import java.util.*;

import cn.newfeifan.mall.module.distri.controller.admin.applicationforwithdrawal.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.applicationforwithdrawal.ApplicationForWithdrawalDO;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;

import cn.newfeifan.mall.module.distri.dal.mysql.applicationforwithdrawal.ApplicationForWithdrawalMapper;

import static cn.newfeifan.mall.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.newfeifan.mall.module.distri.enums.ErrorCodeConstants.*;

/**
 * 提现申请记录 Service 实现类
 *
 * @author 非繁人
 */
@Service
@Validated
public class ApplicationForWithdrawalServiceImpl implements ApplicationForWithdrawalService {

    @Resource
    private ApplicationForWithdrawalMapper applicationForWithdrawalMapper;

    @Resource
    private MemberUserService userService;

    @Override
    public Long createApplicationForWithdrawal(ApplicationForWithdrawalSaveReqVO createReqVO) {
        // 插入
        ApplicationForWithdrawalDO applicationForWithdrawal = BeanUtils.toBean(createReqVO, ApplicationForWithdrawalDO.class);
        applicationForWithdrawalMapper.insert(applicationForWithdrawal);
        // 返回
        return applicationForWithdrawal.getId();
    }

    @Override
    public void updateApplicationForWithdrawal(ApplicationForWithdrawalSaveReqVO updateReqVO) {
        // 校验存在
        validateApplicationForWithdrawalExists(updateReqVO.getId());
        // 更新
        ApplicationForWithdrawalDO updateObj = BeanUtils.toBean(updateReqVO, ApplicationForWithdrawalDO.class);
        applicationForWithdrawalMapper.updateById(updateObj);
    }

    @Override
    public void deleteApplicationForWithdrawal(Long id) {
        // 校验存在
        validateApplicationForWithdrawalExists(id);
        // 删除
        applicationForWithdrawalMapper.deleteById(id);
    }

    private void validateApplicationForWithdrawalExists(Long id) {
        if (applicationForWithdrawalMapper.selectById(id) == null) {
            throw exception(APPLICATION_FOR_WITHDRAWAL_NOT_EXISTS);
        }
    }

    @Override
    public ApplicationForWithdrawalDO getApplicationForWithdrawal(Long id) {
        return applicationForWithdrawalMapper.selectById(id);
    }

    @Override
    public PageResult<ApplicationForWithdrawalDO> getApplicationForWithdrawalPage(ApplicationForWithdrawalPageReqVO pageReqVO) {
        return applicationForWithdrawalMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ApplicationForWithdrawalChannelExcelRespVO> getApplicationForWithdrawalChannelPage(Long id) {

        List<ApplicationForWithdrawalDO> applicationForWithdrawalDOS = applicationForWithdrawalMapper.selectList(ApplicationForWithdrawalDO::getApplicationForWithdrawalChannelId, id, ApplicationForWithdrawalDO::getStatus, 0);

        List<ApplicationForWithdrawalChannelExcelRespVO> list = new ArrayList<>();

        for (ApplicationForWithdrawalDO withdrawalDO : applicationForWithdrawalDOS) {
            MemberUserDO user = userService.getUser(withdrawalDO.getUserId());

            // 定义日期时间格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

            ApplicationForWithdrawalChannelExcelRespVO build = ApplicationForWithdrawalChannelExcelRespVO.builder()
                    .amount(String.valueOf(withdrawalDO.getAmount() / DistriConstants.ONE_HUNDRED))
                    .account(withdrawalDO.getWithdrawalAccount())
                    .mark("提现日期: " + formatter.format(withdrawalDO.getCreateTime()))
                    .build();

            if(withdrawalDO.getWithdrawalType().equals(WithdrawalChannelTypeEnum.ALIPAY.getType())){
                build.setName(user.getAlipayName());
            }else if(withdrawalDO.getWithdrawalType().equals(WithdrawalChannelTypeEnum.BANK.getType())){
                build.setName(user.getAccountName());
            }

            list.add(build);
        }

        return list;
    }

    @Override
    public List<ApplicationForWithdrawalDO> selectListByChannelId(Long id) {
        LambdaQueryWrapper<ApplicationForWithdrawalDO> wrapper = new LambdaQueryWrapperX<ApplicationForWithdrawalDO>()
                .eq(ApplicationForWithdrawalDO::getApplicationForWithdrawalChannelId, id)
                .apply("(status = 0 or status = 2)");
        return applicationForWithdrawalMapper.selectList(wrapper);
    }

    @Override
    public List<ApplicationForWithdrawalDO> selectList() {
        return applicationForWithdrawalMapper.selectList();
    }

    @Override
    public List<GZHXExcelResp> exportGZHXExcel() {
        return applicationForWithdrawalMapper.selectStudy();
    }

}