package com.hzs.report.member.provider;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EApprovalBusiness;
import com.hzs.common.core.enums.EApproveStatus;
import com.hzs.common.domain.report.stat.StatisticalLog;
import com.hzs.common.util.TransactionUtils;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.report.stat.dto.StatisticalLogDTO;
import com.hzs.report.member.service.IStatisticalLogService;
import com.hzs.report.member.vo.NoticeMemberVO;
import com.hzs.report.stat.IStatisticalLogServiceApi;
import com.hzs.system.base.INoticeMemberServiceApi;
import com.hzs.system.base.dto.NoticeMemberDTO;
import com.hzs.system.sys.dto.ApprovalBusinessResultDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 直推数据-发送站内信涉及dubbo服务
 *
 * @author zhangjing
 */
@Slf4j
@DubboService
public class StatisticalLogServiceProvider implements IStatisticalLogServiceApi {

    @Autowired
    private IStatisticalLogService iStatisticalLogService;
    @DubboReference
    private IMemberServiceApi iMemberServiceApi;
    @DubboReference
    private INoticeMemberServiceApi iNoticeMemberServiceApi;


    /**
     * @description: 直推数据-发送站内信审批列表
     * @author: zhang jing
     * @date: 2023/12/21 14:51
     * @param: [eApprovalBusiness, businessCode, pkCountry]
     * @return: com.hzs.common.core.domain.R<java.util.List < com.hzs.report.stat.dto.StatisticalLogDTO>>
     **/
    @Override
    public R<List<StatisticalLogDTO>> queryStatLogList(EApprovalBusiness eApprovalBusiness, String businessCode, Integer pkCountry) {
        QueryWrapper<StatisticalLog> queryWrapper = new QueryWrapper();
        queryWrapper.eq("EDIT_TYPE", eApprovalBusiness.getValue());
        queryWrapper.eq("BUSINESS_NO", businessCode);
        queryWrapper.eq("PK_COUNTRY", pkCountry);
        List<StatisticalLog> list = iStatisticalLogService.list(queryWrapper);
        List<StatisticalLogDTO> statList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(list)) {
            NoticeMemberVO nmvo = JSONObject.parseObject(list.get(0).getOperationContent(), NoticeMemberVO.class);
            for (Long l : nmvo.getPkMemberList()) {
                StatisticalLogDTO stat = BeanUtil.copyProperties(nmvo, StatisticalLogDTO.class);
                stat.setMemberCode(iMemberServiceApi.getMemberById(l).getData().getMemberCode());
                statList.add(stat);
            }
        }
        return R.ok(statList);
    }

    /**
     * @description: 直推数据-发送站内信-审核
     * @author: zhang jing
     * @date: 2023/12/21 16:51
     * @param: [approvalBusinessResultDTO]
     * @return: com.hzs.common.core.domain.R
     **/
    @Transactional(rollbackFor = Exception.class)
    @Override
    public R sendingMessagesProcess(ApprovalBusinessResultDTO approvalBusinessResultDTO) {
        QueryWrapper<StatisticalLog> queryWrapper = new QueryWrapper();
        queryWrapper.eq("APPROVE_STATUS", EApproveStatus.ALREADY_SUBMIT.getValue());
        queryWrapper.eq("BUSINESS_NO", approvalBusinessResultDTO.getBusinessCode());
        List<StatisticalLog> list = iStatisticalLogService.list(queryWrapper);
        if (CollectionUtil.isEmpty(list)) {
            log.info("直推数据-发送站内信：签呈不存在");
            return R.fail(TransactionUtils.getContent(ConfigMsgConstants.PETITION_ABSENT_ERROR));
        } else {
            Date date = new Date();
            Long userID = approvalBusinessResultDTO.getLoginUser().getUserId();
            //审核通过
            if (approvalBusinessResultDTO.getEApproveStatus().getValue() == EApproveStatus.FINISH.getValue()) {
                NoticeMemberVO nmvo = JSONObject.parseObject(list.get(0).getOperationContent(), NoticeMemberVO.class);
                NoticeMemberDTO nmDto = BeanUtil.copyProperties(nmvo, NoticeMemberDTO.class);
                R mn = iNoticeMemberServiceApi.saveMemberNotice(nmDto);
                if (mn.isSuccess()) {
                    //修改日志状态
                    UpdateWrapper<StatisticalLog> ststUp = new UpdateWrapper<>();
                    ststUp.set("APPROVE_STATUS", EApproveStatus.FINISH.getValue());
                    ststUp.set("PK_MODIFIED", userID);
                    ststUp.set("MODIFIED_TIME", date);
                    ststUp.eq("BUSINESS_NO", approvalBusinessResultDTO.getBusinessCode());
                    iStatisticalLogService.update(ststUp);
                }
            } else {
                UpdateWrapper<StatisticalLog> ststUp = new UpdateWrapper<>();
                ststUp.set("APPROVE_STATUS", EApproveStatus.REJECTED.getValue());
                ststUp.set("PK_MODIFIED", userID);
                ststUp.set("MODIFIED_TIME", date);
                ststUp.eq("BUSINESS_NO", approvalBusinessResultDTO.getBusinessCode());
                iStatisticalLogService.update(ststUp);
            }
        }
        return R.ok();
    }
}
