package com.hzs.member.self.controller.manage;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.self.service.ICuSelfServiceLogService;
import com.hzs.member.self.vo.SelfModifyAddressVO;
import com.hzs.system.base.IAreaServiceApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 自助服务-修改收货地址
 **/
@Slf4j
@RequestMapping("/manage/self-modify-address")
@RestController
public class SelfModifyAddressController extends BaseController {

    @Autowired
    private ICuSelfServiceLogService serviceLogService;

    @DubboReference
    IAreaServiceApi iAreaServiceApi;


    /**
     * 自助服务-查询已申请修改收货地址
     **/
    @Log(module = EOperationModule.MODIFY_ADDRESS, business = EOperationBusiness.MODIFY_ADDRESS, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(SelfModifyAddressVO selfModifyAddressVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        selfModifyAddressVO.setPkCountry(pkCountry);
        selfModifyAddressVO.setEditType(EApprovalBusiness.SELF_MODIFY_ADDRESS.getValue());
        startPage();
        List<SelfModifyAddressVO> slList = serviceLogService.querySelfModifyAddressManageList(selfModifyAddressVO);
        if (CollectionUtil.isNotEmpty(slList)) {
            // 调用system服务，获取地址
            Map<Integer, String> areaMap = iAreaServiceApi.getAreaMap(pkCountry).getData();
            // 查询到的订单对应的订单明细列表
            if (CollectionUtil.isNotEmpty(slList)) {
                for (SelfModifyAddressVO sa : slList) {
                    SelfModifyAddressVO csso = JSONObject.parseObject(sa.getNewValue(), SelfModifyAddressVO.class);
                    sa.setNewRecName(csso.getNewRecName());
                    sa.setNewRecPhone(csso.getNewRecPhone());
                    sa.setNewRecProvince(csso.getNewRecProvince());
                    sa.setNewRecCity(csso.getNewRecCity());
                    sa.setNewRecCounty(csso.getNewRecCounty());
                    sa.setNewRecAddress(csso.getNewRecAddress());
                    sa.setRecProvinceVal(areaMap.get(sa.getRecProvince()));
                    sa.setRecCityVal(areaMap.get(sa.getRecCity()));
                    if (StringUtils.isNotNull(sa.getRecCounty())) {
                        sa.setRecCountyVal(areaMap.get(sa.getRecCounty()));
                    }
                    sa.setNewRecProvinceVal(areaMap.get(sa.getNewRecProvince()));
                    sa.setNewRecCityVal(areaMap.get(sa.getNewRecCity()));
                    if (StringUtils.isNotNull(sa.getNewRecCounty())) {
                        sa.setNewRecCountyVal(areaMap.get(sa.getNewRecCounty()));
                    }
                }
            }
        }

        TableDataInfo tableDataInfo = getDataTable(slList);
        tableDataInfo.setRows(slList);
        return tableDataInfo;
    }

}
