package com.hzs.system.base.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.MagicNumberConstants;
import com.hzs.common.core.enums.EApprovalBusiness;
import com.hzs.common.core.enums.EApproveRechargeStatus;
import com.hzs.common.core.enums.ENoticeBusinessType;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.system.base.BdPlatformMessage;
import com.hzs.common.domain.system.base.BdPlatformMessageDetail;
import com.hzs.common.domain.system.ext.BdPlatformMessageDetailExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.base.mapper.BdPlatformMessageMapper;
import com.hzs.system.base.service.IBdPlatformMessageDetailService;
import com.hzs.system.base.service.IBdPlatformMessageService;
import com.hzs.system.base.vo.BdPlatformMessageDetailVO;
import com.hzs.system.base.vo.BdPlatformMessageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 后台站内信 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2023-04-04
 */
@Service
public class BdPlatformMessageServiceImpl extends ServiceImpl<BdPlatformMessageMapper, BdPlatformMessage> implements IBdPlatformMessageService {

    @Autowired
    IBdPlatformMessageDetailService bdPlatformMessageDetailService;

    @Override
    public Integer savePlateFormMessage(BdPlatformMessageVO messageVO) {
        BdPlatformMessage bdPlatformMessage = BeanUtil.copyProperties(messageVO, BdPlatformMessage.class);
        bdPlatformMessage.setPkCreator(messageVO.getPkMember());
        return baseMapper.insert(bdPlatformMessage);
    }

    @Override
    public Integer deletePlateFormMessageById(Long pkId) {
        BdPlatformMessage platformMessage = new BdPlatformMessage();
        platformMessage.setPkId(pkId);
        platformMessage.setPkModified(SecurityUtils.getUserId());
        return baseMapper.deletePlateFormMessageById(platformMessage);

    }

    @Override
    public List<BdPlatformMessageVO> showPlatformMessage(BdPlatformMessageVO platformMessageVO) {
        List<BdPlatformMessage> bdPlatformMessages = baseMapper.showPlatformMessage(platformMessageVO);
        return BeanUtil.copyToList(bdPlatformMessages, BdPlatformMessageVO.class);
    }

    @Override
    public Boolean approvalPushInfo(List<BdPlatformMessageDetailVO> bdPlatformMessageDetailVOList) {
        Boolean result = false;
        BdPlatformMessageVO bdPlatformMessageVO;
        for (BdPlatformMessageDetailVO bdPlatformMessageDetailVO : bdPlatformMessageDetailVOList) {
            bdPlatformMessageVO = BdPlatformMessageVO.builder().pkCountry(bdPlatformMessageDetailVO.getPkCountry())
                    .businessType(bdPlatformMessageDetailVO.getNoticeBusinessType().getValue())
                    .approvalType(bdPlatformMessageDetailVO.getApproveRechargeStatus().getValue()).build();
            BdPlatformMessage platformMessage = baseMapper.selectPlatformMessageByBusinessAndApproveType(bdPlatformMessageVO);
            Integer value = bdPlatformMessageDetailVO.getApprovalBusiness().getValue();
            bdPlatformMessageDetailVO.setBusinessCode(value.toString());
            result = batchSavePlatformMessageDetail(bdPlatformMessageDetailVO, platformMessage);
            if(!result){
                break;
            }
        }
        return result;
    }

    /**
     * 批量保存后台站内信详情
     *
     * @param bdPlatformMessageDetailVO 站内信详情
     * @param platformMessage           站内信
     * @return Integer
     */
    private Boolean batchSavePlatformMessageDetail(BdPlatformMessageDetailVO bdPlatformMessageDetailVO, BdPlatformMessage platformMessage) {
        BdPlatformMessageDetail detail;
        List<BdPlatformMessageDetail> detailVOList = new ArrayList<>();
        if (null != platformMessage) {
            Long pkId = platformMessage.getPkId();
            List<Long> pkMembers = bdPlatformMessageDetailVO.getPkMembers();
            for (Long pkMember : pkMembers) {
                detail = BeanUtil.copyProperties(bdPlatformMessageDetailVO, BdPlatformMessageDetail.class);
                detail.setPkMember(pkMember);
                detail.setPkMessage(pkId);
                detailVOList.add(detail);
            }
            return bdPlatformMessageDetailService.saveBatch(detailVOList);
        }
        return false;
    }

    @Override
    public Object[] selectAdminPushInfo(BdPlatformMessageVO platformMessageVO) {
        BdPlatformMessageVO messageVO;
        List<BdPlatformMessageVO> platformMessageVOList = new ArrayList<>();
        List<BdPlatformMessageDetailExt> bdPlatformMessageDetailList = bdPlatformMessageDetailService.selectPlatformMessageDetailByPkMember(platformMessageVO.getPkCountry(), platformMessageVO.getPkMember());
        if (CollUtil.isNotEmpty(bdPlatformMessageDetailList)) {
            for (BdPlatformMessageDetailExt messageDetailExt : bdPlatformMessageDetailList) {
                messageVO = BdPlatformMessageVO.builder().title(messageDetailExt.getTitle())
                        .content(getPushContent(messageDetailExt)).url(getUrl(messageDetailExt))
                        .readState(messageDetailExt.getReadState()).pkId(messageDetailExt.getPkId())
                        .creationTime(messageDetailExt.getCreationTime()).build();

                platformMessageVOList.add(messageVO);
            }
        }
        Object[] objects = new Object[]{bdPlatformMessageDetailList, platformMessageVOList};
        return objects;
    }

    /**
     * 获取url
     *
     * @param messageDetailExt
     * @return String
     */
    private String getUrl(BdPlatformMessageDetailExt messageDetailExt) {
        String index = "0";
        String url = messageDetailExt.getUrl();
        StringBuilder sb = new StringBuilder(url);
        Integer approvalType = messageDetailExt.getApprovalType();
        if (!approvalType.equals(EApproveRechargeStatus.WAIT.getValue())) {
            index = "1";
        }
        sb.append("?id=").append(messageDetailExt.getApprovalCode()).append("&index=")
                .append(index);
        return sb.toString();

    }

    /**
     * 转换推送内容
     *
     * @param messageDetailExt 站内信信息
     * @return String
     */
    private String getPushContent(BdPlatformMessageDetailExt messageDetailExt) {
        String content = null;
        Integer approvalType = messageDetailExt.getApprovalType();
        Integer businessType = messageDetailExt.getBusinessType();
        String templateContent = messageDetailExt.getContent();
        String submitName = messageDetailExt.getSubmitName();
        String submitTime = DateUtils.parseDateTimeToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, messageDetailExt.getSubmitTime());
        String submitDept = messageDetailExt.getSubmitDept();
        //签呈
        if (businessType.equals(ENoticeBusinessType.PETITION.getValue())) {
            String businessCode = messageDetailExt.getBusinessCode();
            String label = EApprovalBusiness.getEnumByValue(Integer.parseInt(businessCode)).getLabel();
            if (approvalType.equals(EApproveRechargeStatus.WAIT.getValue())) {
                //张三（信息中心）在2023-02-21 9时提交的【修改安置编号】待审核，点击查看详情！
                content = String.format(templateContent, submitName, submitDept, submitTime, label);
            }
            if (approvalType.equals(EApproveRechargeStatus.AGREE.getValue())) {
                //您在2023-02-21 9时提交的【修改安置编号】已通过审核，点击查看详情！
                content = String.format(templateContent, submitTime, label);
            }
            if (approvalType.equals(EApproveRechargeStatus.REJECT.getValue())) {
                String rejectName = messageDetailExt.getRejectName();
                String rejectDept = messageDetailExt.getRejectDept();
                //您在2023-02-21 9时提交的【修改安置编号】被 张三（财务部）驳回，点击查看详情
                content = String.format(templateContent, submitTime, label, rejectName, rejectDept);
            }

        }
        return content;
    }


}
