package com.hzs.member.address.controller.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.constant.msg.CommonMsgConstants;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.member.account.CuMemberAuthentication;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.member.account.service.ICuMemberAuthenticationService;
import com.hzs.member.address.param.ApiMemberGiftParam;
import com.hzs.member.address.vo.ApiMemberGiftVO;
import com.hzs.system.base.IAreaServiceApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @BelongsProject: hzs_cloud
 * @BelongsPackage: com.hzs.member.address.controller.api
 * @Author: yh
 * @CreateTime: 2023-05-09  15:15
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api/member-gift")
public class ApiMemberGiftController extends BaseController {

    @DubboReference
    private IAreaServiceApi iAreaServiceApi;

    @Autowired
    private ICuMemberAuthenticationService iCuMemberAuthenticationService;

    /**
     * 查询会员收货地址
     *
     * @return
     */
    @GetMapping("/member-gift-list")
    public AjaxResult memberGiftList() {
        QueryWrapper<CuMemberAuthentication> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_MEMBER", SecurityUtils.getUserId());
        queryWrapper.orderByDesc(SystemFieldConstants.CREATION_TIME);
        List<CuMemberAuthentication> list = iCuMemberAuthenticationService.list(queryWrapper);

        List<ApiMemberGiftVO> resultList = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(list)) {
            // 省市县显示
            Map<Integer, String> areaMap = iAreaServiceApi.getAreaMap(SecurityUtils.getPkCountry()).getData();

            for (CuMemberAuthentication authentication : list) {
                ApiMemberGiftVO memberGiftVO = BeanUtil.copyProperties(authentication, ApiMemberGiftVO.class);

                if (authentication.getPkProvince() != null) {
                    memberGiftVO.setPkProvinceVal(areaMap.get(authentication.getPkProvince()));
                }
                if (authentication.getPkCity() != null) {
                    memberGiftVO.setPkCityVal(areaMap.get(authentication.getPkCity()));
                }
                if (authentication.getPkCounty() != null) {
                    memberGiftVO.setPkCountyVal(areaMap.get(authentication.getPkCounty()));
                }
                resultList.add(memberGiftVO);
            }
        }

        return AjaxResult.success(resultList);
    }

    /**
     * 修改礼品地址
     *
     * @param param
     * @return
     */
    @PostMapping("/update-gift")
    public AjaxResult updateGift(@RequestBody ApiMemberGiftParam param) {
        if (null == param.getPkId() || null == param.getPkProvince() || null == param.getPkCity()
                || StringUtils.isEmpty(param.getAddress())) {
            // 缺少参数
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }

        UpdateWrapper<CuMemberAuthentication> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq(SystemFieldConstants.PK_ID, param.getPkId());
        updateWrapper.eq("PK_MEMBER", SecurityUtils.getUserId());
        updateWrapper.set("PK_PROVINCE", param.getPkProvince());
        updateWrapper.set("PK_CITY", param.getPkCity());
        updateWrapper.set("PK_COUNTY", param.getPkCounty());
        updateWrapper.set("ADDRESS", param.getAddress());
        updateWrapper.set(SystemFieldConstants.PK_MODIFIED, SecurityUtils.getUserId());
        updateWrapper.set(SystemFieldConstants.MODIFIED_TIME, new Date());
        return toAjax(iCuMemberAuthenticationService.update(updateWrapper));
    }

}
