package com.hzs.member.self.provider;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EApprovalBusiness;
import com.hzs.common.core.utils.StringUtils;

import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.domain.member.self.CuSelfServiceLog;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.member.self.ICuSelfServiceServiceApi;
import com.hzs.member.self.dto.SelfServiceDataDTO;
import com.hzs.member.self.service.ICuSelfServiceLogService;
import com.hzs.member.self.vo.SelfModifyAddressVO;
import com.hzs.system.base.IAreaServiceApi;
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 java.util.Map;

/**
 * 自助服务Dubbo 服务
 */
@Slf4j
@DubboService
public class ICuSelfServiceServiceProvider implements ICuSelfServiceServiceApi {

    @Autowired
    ICuSelfServiceLogService cuSelfServiceLogService;
    @Autowired
    private ICuSelfServiceLogService serviceLogService;
    @Autowired
    private ICuMemberService cuMemberService;

    @DubboReference
    IAreaServiceApi iAreaServiceApi;

    @Override
    public R<?> analysisSelfServiceData(SelfServiceDataDTO selfServiceDataDTO) {
        EApprovalBusiness eApprovalBusiness = EApprovalBusiness.getEnumByValue(selfServiceDataDTO.getEditType());
        switch (eApprovalBusiness) {
            case SELF_MODIFY_ADDRESS:
                //修改地址
                return R.ok(getSelfModifyAddress(selfServiceDataDTO));
            default:
                return R.fail();
        }
    }

    /**
     * 自助服务-修改收货地址
     **/
    private SelfModifyAddressVO getSelfModifyAddress(SelfServiceDataDTO selfServiceDataDTO) {
        SelfModifyAddressVO selMad = SelfModifyAddressVO.builder()
                .businessNo(selfServiceDataDTO.getBusinessNo())
                .pkCountry(selfServiceDataDTO.getPkCountry())
                .editType(selfServiceDataDTO.getEditType())
                .build();
        LambdaQueryWrapper<CuSelfServiceLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CuSelfServiceLog::getBusinessNo, selfServiceDataDTO.getBusinessNo());
        CuSelfServiceLog serviceLog = serviceLogService.getOne(queryWrapper);
        if (null != serviceLog) {
            // 调用system服务，获取地址
            Map<Integer, String> areaMap = iAreaServiceApi.getAreaMap(selfServiceDataDTO.getPkCountry()).getData();

            SelfModifyAddressVO sad = JSONObject.parseObject(serviceLog.getNewValue(), SelfModifyAddressVO.class);
            SelfModifyAddressVO oldsad = JSONObject.parseObject(serviceLog.getOldValue(), SelfModifyAddressVO.class);
            //旧值
            String recCountyVal = "";
            sad.setOrderCode(serviceLog.getOrderCode());
            CuMember member = cuMemberService.getMember(serviceLog.getPkMember());
            sad.setMemberName(member.getMemberName());
            sad.setMemberCode(member.getMemberCode());
            sad.setRecName(oldsad.getRecName());
            sad.setRecPhone(oldsad.getRecPhone());
            if (StringUtils.isNotNull(oldsad.getRecCounty())) {
                recCountyVal = areaMap.get(oldsad.getRecCounty());
            }
            sad.setRecAddress(areaMap.get(oldsad.getRecProvince())
                    + areaMap.get(oldsad.getRecCity())
                    + recCountyVal
                    + oldsad.getRecAddress());
            //新值
            if (StringUtils.isNotNull(sad.getRecCounty())) {
                recCountyVal = areaMap.get(sad.getNewRecCounty());
            }
            sad.setNewRecAddress(areaMap.get(sad.getNewRecProvince())
                    + areaMap.get(sad.getNewRecCity())
                    + recCountyVal
                    + sad.getNewRecAddress());
            selMad = BeanUtil.copyProperties(sad, SelfModifyAddressVO.class);

        }
        return selMad;
    }


    @Override
    public R<?> onlineApproval(ApprovalBusinessResultDTO approvalBusinessResultDTO) {
        //通过/驳回（EApproveOperation）
        Integer approveOperation = approvalBusinessResultDTO.getApproveOperation();
        EApprovalBusiness eApprovalBusiness = approvalBusinessResultDTO.getEApprovalBusiness();
        String businessCode = approvalBusinessResultDTO.getBusinessCode();
        if (StringUtils.isEmpty(businessCode) || null == approveOperation || null == eApprovalBusiness) {
            return R.fail("提交审核缺少参数！");
        }
        CuSelfServiceLog selfServiceLog = cuSelfServiceLogService.selectSelfServiceByBusinessNo(eApprovalBusiness.getValue(), businessCode);
        if (null == selfServiceLog) {
            return R.fail("审批记录不存在！");
        }
        try {
            switch (eApprovalBusiness) {
                case SELF_MODIFY_ADDRESS:
                    //修改地址
                    return R.ok(cuSelfServiceLogService.selfExamine(approvalBusinessResultDTO));
                default:
                    return R.fail();
            }

        } catch (Exception e) {
            log.error("审核状态为： {} 的审核出错", approveOperation, e);
            return R.fail("审批自助服务流程出错！");
        }
    }

}
