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

import cn.hutool.core.collection.CollectionUtil;
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.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.core.utils.TransactionUtils;
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 org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 会员更名、密码修改表 服务实现类
 */
@Service
public class CuMemberServiceLogServiceImpl extends ServiceImpl<CuMemberServiceLogMapper, CuMemberServiceLog> implements ICuMemberServiceLogService {

    @Override
    public Integer selectByMaxApproveNumber(Long pkMember, int editType, Integer approveStatus) {
        return baseMapper.selectByMaxApproveNumber(pkMember, editType, approveStatus);
    }

    @Override
    public List<CuMemberServiceLogVO> selectServiceLogList(CuMemberServiceLogVO cuMemberServiceLogVO) {
        return baseMapper.selectServiceLogList(cuMemberServiceLogVO);
    }

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

    @Override
    public List<BusinessMemberVO> selectOldValue(BusinessMemberVO businessMemberVO) {
        return baseMapper.selectOldValue(businessMemberVO);
    }

    @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(TransactionUtils.getContent(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);
    }

    @Override
    public R<CuMemberServiceLog> selectByBusinessNo(String businessNo) {
        QueryWrapper<CuMemberServiceLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("BUSINESS_NO", businessNo);
        return R.ok(baseMapper.selectOne(queryWrapper));
    }

    @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);
    }

    @Override
    public List<CuMemberServiceLog> queryList(Integer editType, String memberCode, String memberName, Integer pkCountry) {
        return baseMapper.queryList(editType, memberCode, memberName, pkCountry);
    }

    @Override
    public List<CuMemberServiceLogExt> selectByList(CuMemberServiceLogExt memberServiceLogExt) {
        return baseMapper.selectByList(memberServiceLogExt);
    }

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