package com.hzs.member.detail.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.core.enums.EOrderPrefix;
import com.hzs.common.core.exception.ServiceException;
import com.hzs.common.core.utils.CommonUtil;
import com.hzs.common.domain.member.detail.CuMemberServiceLog;
import com.hzs.common.domain.member.detail.CuMemberServiceLogExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.detail.mapper.CuMemberServiceLogMapper;
import com.hzs.member.detail.service.ICuMemberServiceLogService;
import com.hzs.member.detail.vo.CuMemberServiceLogVO;
import com.hzs.member.handlebusiness.vo.BusinessMemberVO;
import com.hzs.member.self.vo.DomicileChangeVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 会员更名、密码修改表 服务实现类
 *
 * @author hzs
 * @since 2022-08-22
 */
@Service
public class CuMemberServiceLogServiceImpl extends ServiceImpl<CuMemberServiceLogMapper, CuMemberServiceLog> implements ICuMemberServiceLogService {
    /**
     * 查询最大审核次数
     *
     * @param pkMember
     * @param editType
     * @return
     */
    @Override
    public Integer selectByMaxApproveNumber(Long pkMember, Integer editType, Integer approveStatus) {
        return baseMapper.selectByMaxApproveNumber(pkMember, editType, approveStatus);
    }

    /**
     * @description: 查询会员办理业务日志最新数据
     * @author: zhang jing
     * @date: 2023/3/24 9:08
     * @param: [pkMember, editType, approveStatus]
     * @return: com.hzs.member.detail.vo.CuMemberServiceLogVO
     **/
    @Override
    public CuMemberServiceLog selectTopData(Long pkMember, int editType, int approveStatus) {
        return baseMapper.selectTopData(pkMember, editType, approveStatus);
    }


    /**
     * @description: 查询用户是否已经存在未审核业务
     * @author: zhang jing
     * @date: 2023/1/29 17:26
     * @param: [cuMemberServiceLogVO]
     * @return: java.util.List<com.hzs.common.domain.member.detail.CuMemberServiceLog>
     **/
    @Override
    public List<CuMemberServiceLogVO> selectServiceLogList(CuMemberServiceLogVO cuMemberServiceLogVO) {
        return baseMapper.selectServiceLogList(cuMemberServiceLogVO);
    }

    /**
     * 查询
     *
     * @param cuMemberServiceLog
     * @return
     */
    @Override
    public List<CuMemberServiceLogExt> selectByServiceLogList(CuMemberServiceLog cuMemberServiceLog) {
        return baseMapper.selectByServiceLogList(cuMemberServiceLog);
    }

    /**
     * @description: 查询审核通过的最新数据
     * @author: zhang jing
     * @date: 2023/1/30 10:02
     * @param: [cuMemberServiceLogVO]
     * @return: com.hzs.member.detail.vo.CuMemberServiceLogVO
     **/
    @Override
    public CuMemberServiceLogVO selectNewestServiceLog(CuMemberServiceLogVO cuMemberServiceLogVO) {
        return baseMapper.selectNewestServiceLog(cuMemberServiceLogVO);
    }

    /**
     * @description: 查询会员原值
     * @author: zhang jing
     * @date: 2023/1/30 14:20
     * @param: [cuMemberOldValueVO]
     * @return: java.util.List<com.hzs.member.handleBusiness.vo.CuMemberOldValueVO>
     **/
    @Override
    public List<BusinessMemberVO> selectOldValue(BusinessMemberVO businessMemberVO) {
        return baseMapper.selectOldValue(businessMemberVO);
    }

    /**
     * @description: 再次发起签呈
     * @author: zhang jing
     * @date: 2023/2/2 9:09
     * @param: [cmslList]
     * @return: boolean
     **/
    @Transactional(rollbackFor = Exception.class)
    @Override
    public CuMemberServiceLogVO againBusiness(List<CuMemberServiceLog> cmslList, CuMemberServiceLogVO cuMemberServiceLogVO) {
        Date date = new Date();
        Long userID = SecurityUtils.getUserId();
        Integer pkCountry = SecurityUtils.getPkCountry();
        List<CuMemberServiceLog> newCmslList = new ArrayList<>();
        //业务单号
        String code = CommonUtil.createSerialNumber(EOrderPrefix.HANDLE_BUSINESS.getValue());
        cuMemberServiceLogVO.setBusinessNo(code);
        for (CuMemberServiceLog csl : cmslList) {
            CuMemberServiceLogVO csv = new CuMemberServiceLogVO();
            csv.setPkMember(csl.getPkMember());
            csv.setEditType(csl.getEditType());
            List<CuMemberServiceLogVO> cslList = selectServiceLogList(csv);
            if (CollectionUtil.isNotEmpty(cslList)) {
                throw new ServiceException(ConfigMsgConstants.MEMBER_UPDATE_CODE_REPEAT);
            }
            csl.setCreationTime(date);
            csl.setPkCreator(userID);
            csl.setPkCountry(pkCountry);
            csl.setBusinessNo(code);
            csl.setApproveStatus(EApproveStatus.WAIT_SUBMIT.getValue());
            baseMapper.insert(csl);
            newCmslList.add(csl);
        }
        cuMemberServiceLogVO.setCslList(newCmslList);
        return cuMemberServiceLogVO;
    }

    @Override
    public List<CuMemberServiceLog> queryMaxCuMemberServiceLog(Long pkMember, List<Integer> editTypeList) {
        return baseMapper.queryMaxCuMemberServiceLog(pkMember, editTypeList);
    }

    /**
     * 根据业务单号查询
     *
     * @return
     */
    @Override
    public R<CuMemberServiceLog> selectByBusinessNo(String businessNo) {
        QueryWrapper<CuMemberServiceLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("BUSINESS_NO", businessNo);
        return R.ok(baseMapper.selectOne(queryWrapper));
    }

    /**
     * 根据业务单号修改信息
     *
     * @param memberLog
     * @return
     */
    @Override
    public R<Boolean> updateByBusinessNo(CuMemberServiceLog memberLog) {
        UpdateWrapper<CuMemberServiceLog> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("BUSINESS_NO", memberLog.getBusinessNo());
        return R.ok(baseMapper.update(memberLog, updateWrapper) == 1);
    }

    /**
     * 查询列表信息
     *
     * @param editType
     * @param memberCode
     * @param memberName
     * @param pkCountry
     * @param approveStatus
     * @return
     */
    @Override
    public List<CuMemberServiceLog> queryList(Integer editType, String memberCode, String memberName, Integer pkCountry, Integer approveStatus) {
        return baseMapper.queryList(editType, memberCode, memberName, pkCountry, approveStatus);
    }

    /**
     * @description: 根据业务单号查询业务日志
     * @author: zhang jing
     * @date: 2023/7/29 11:16
     * @param: [cuMemberServiceLog]
     * @return: java.util.List<com.hzs.common.domain.member.detail.CuMemberServiceLog>
     **/
    @Override
    public R<List<CuMemberServiceLog>> selectCuMemberServiceLog(String businessNo) {
        QueryWrapper<CuMemberServiceLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("BUSINESS_NO", businessNo);
        return R.ok(baseMapper.selectList(queryWrapper));
    }

    /**
     * 查询展示列表
     *
     * @return
     */
    @Override
    public List<CuMemberServiceLog> selectByList(CuMemberServiceLogExt memberServiceLogExt) {
        return baseMapper.selectByList(memberServiceLogExt);
    }

    @Override
    public List<CuMemberServiceLog> selectServiceValue(CuMemberServiceLogExt cuMemberServiceLog) {
        return baseMapper.selectServiceValue(cuMemberServiceLog);
    }

    @Override
    public CuMemberServiceLogExt queryPolicy(Long pkMember) {
        return baseMapper.queryPolicy(pkMember);
    }

    /**
     * @description: 查询会员办理业务日志最新数据
     * @author: zhang jing
     * @date: 2023/3/24 9:08
     * @param: [pkMember, editType, approveStatus]
     * @return: com.hzs.member.detail.vo.CuMemberServiceLogVO
     **/
    @Override
    public CuMemberServiceLogVO selectTopDataList(Long pkMember, List<Integer> editType, int approveStatus,String actEndDateStr) {
        CuMemberServiceLogVO cmvo=new CuMemberServiceLogVO();
        List<CuMemberServiceLog> cmsList=baseMapper.selectTopDataList(pkMember, editType, approveStatus,actEndDateStr);
        if(CollectionUtil.isNotEmpty(cmsList)){
            List<CuMemberServiceLog> sortedCmsList = cmsList.stream()
                    .sorted(Comparator.comparing(CuMemberServiceLog::getCreationTime))
                    .collect(Collectors.toList());
            for(CuMemberServiceLog cms: sortedCmsList){
                cms.setPkMember(pkMember);
                if(cms.getEditType()== EApprovalBusiness.CHANGE_NAME.getValue()){
                    cmvo.setOldName(cms.getOldValue());
                }if(cms.getEditType()== EApprovalBusiness.CHANGE_PHONE.getValue()){
                    cmvo.setOldPhone(cms.getOldValue());
                }if(cms.getEditType()== EApprovalBusiness.DOMICILE_CHANGE.getValue()){
                    DomicileChangeVO oldValue = JSONUtil.toBean(cms.getOldValue(), DomicileChangeVO.class);
                    cmvo.setOldPhone(oldValue.getPhone());
                    cmvo.setOldName(oldValue.getMemberName());
                }
            }
        }
        return cmvo;
    }


}
