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


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.enums.EApprovalBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
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.core.web.page.TableDataInfo;
import com.hzs.common.domain.member.address.CuMemberGift;
import com.hzs.common.domain.member.address.CuMemberGiftRecord;
import com.hzs.common.domain.member.address.CuMemberReceiving;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.member.address.service.ICuMemberGiftRecordService;
import com.hzs.member.address.service.ICuMemberGiftService;
import com.hzs.member.address.vo.CuMemberGiftVO;
import com.hzs.member.param.CuMemberGiftParam;
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;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 会员礼包收货地址 前端控制器
 * </p>
 *
 * @author zhangjing
 * @since 2022-10-20
 */
@RestController
@RequestMapping("/manager/member-gift")
public class CuMemberGiftController extends BaseController {
    @DubboReference
    private IAreaServiceApi iAreaServiceApi;
    @Autowired
    ICuMemberGiftService cuMemberGiftService;
    @Autowired
    ICuMemberGiftRecordService cuMemberGiftRecordService;

    /**
     * @description: 查询会员礼包收货地址列表
     * @author: zhang jing
     * @date: 2022/10/20 10:36
     * @param: [memberAuthorityParam]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.MEMBER_AUTHORITY, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(CuMemberGiftParam memberAuthorityParam) {
        startPage();
        List<CuMemberGiftParam> list = cuMemberGiftService.selectCuMemberGiftList(memberAuthorityParam);
        return getDataTable(list);
    }

    /**
     * @description: 修改会员礼包收货地址
     * @author: zhang jing
     * @date: 2022/8/29 9:58
     * @param: [cuMemberGift]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.MEMBER_AUTHORITY, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody CuMemberGift cuMemberGift) {
        if(StringUtils.isNull(cuMemberGift.getPkId())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.CU_MEMBER_GIFT_PKID_NOT_NULL));
        }
        if(StringUtils.isEmpty(cuMemberGift.getGiftAddress())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.GIFT_ADDRESS_NOT_NULL));
        }
        if(StringUtils.isNull(cuMemberGift.getGiftName())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.GIFT_NAME_NOT_NULL));
        }
        if(StringUtils.isNull(cuMemberGift.getGiftPhone())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.GIFT_PHONE_NOT_NULL));
        }
        if(StringUtils.isNull(cuMemberGift.getGiftProvince())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.GIFT_PROVINCE_NOT_NULL));
        }
        if(StringUtils.isNull(cuMemberGift.getGiftCity())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.GIFT_CITY_NOT_NULL));
        }
        if(StringUtils.isNull(cuMemberGift.getGiftCounty())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.GIFT_COUNTY_NOT_NULL));
        }
        cuMemberGift.setPkCountry(SecurityUtils.getPkCountry());
        cuMemberGift.setPkModified(SecurityUtils.getUserId());
        cuMemberGift.setModifiedTime(new Date());
        CuMemberGift cuMemberGiftOld = cuMemberGiftService.getById(cuMemberGift.getPkId());
        JSONObject oldJson = new JSONObject();
        JSONObject newJson = new JSONObject();
        oldJson.put("pkId", cuMemberGiftOld.getPkId());
        oldJson.put("pkMember", cuMemberGiftOld.getPkMember());
        oldJson.put("giftAddress", cuMemberGiftOld.getGiftAddress());
        oldJson.put("giftName", cuMemberGiftOld.getGiftName());
        oldJson.put("giftPhone", cuMemberGiftOld.getGiftPhone());
        oldJson.put("giftProvince", cuMemberGiftOld.getGiftProvince());
        oldJson.put("giftCity", cuMemberGiftOld.getGiftCity());
        oldJson.put("giftCounty", cuMemberGiftOld.getGiftCounty());
        oldJson.put("pkCountry", cuMemberGiftOld.getPkCountry());
        oldJson.put("pkModified", cuMemberGiftOld.getPkModified());
        oldJson.put("modifiedTime", cuMemberGiftOld.getModifiedTime());
        String oldStr = oldJson.toJSONString();
        newJson.put("pkId", cuMemberGift.getPkId());
        newJson.put("pkMember", cuMemberGiftOld.getPkMember());
        newJson.put("giftAddress", cuMemberGift.getGiftAddress());
        newJson.put("giftName", cuMemberGift.getGiftName());
        newJson.put("giftPhone", cuMemberGift.getGiftPhone());
        newJson.put("giftProvince", cuMemberGift.getGiftProvince());
        newJson.put("giftCity", cuMemberGift.getGiftCity());
        newJson.put("giftCounty", cuMemberGift.getGiftCounty());
        newJson.put("pkCountry", SecurityUtils.getPkCountry());
        newJson.put("pkModified", SecurityUtils.getUserId());
        newJson.put("modifiedTime", new Date());
        String newStr = newJson.toJSONString();
        CuMemberGiftRecord memberGiftRecord =new CuMemberGiftRecord();
        memberGiftRecord.setPkMember(cuMemberGiftOld.getPkMember());
        memberGiftRecord.setOldValue(oldStr);
        memberGiftRecord.setNewValue(newStr);
        cuMemberGiftRecordService.save(memberGiftRecord);
        CuMemberGiftParam cuMemberGiftParam=new CuMemberGiftParam();
        cuMemberGiftParam.setPkCuMemberGiftRecord(memberGiftRecord.getPkId());
        cuMemberGiftParam.setApprovalType(EApprovalBusiness.CU_MEMBER_GIFT.getValue());
        return AjaxResult.success(memberGiftRecord);
    }

    /**
     * @description: 获取单条会员礼包收货地址
     * @author: zhang jing
     * @date: 2022/8/29 9:58
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.MEMBER_AUTHORITY, method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        CuMemberGift cuMemberGift = cuMemberGiftService.getById(pkId);
        return AjaxResult.success(cuMemberGift);
    }

    /**
     * @description: 查询会员礼包收货地址修改审批详情
     * @author: zhang jing
     * @date: 2022/8/29 9:58
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.MEMBER_AUTHORITY, method = EOperationMethod.SELECT)
    @GetMapping("/approvalDetail/{pkId}")
    public AjaxResult approvalDetail(@PathVariable Long pkId) {
       CuMemberGiftVO memberGiftVO = cuMemberGiftService.approvalDetail(pkId);
        return AjaxResult.success(memberGiftVO);
    }

    /**
     * 查询会员收货地址
     * @return
     */
    @GetMapping("get-member-gift")
    public AjaxResult getMembberGift(){
        QueryWrapper<CuMemberGift> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_MEMBER", SecurityUtils.getUserId());
        List<CuMemberGift> list = cuMemberGiftService.list(queryWrapper);
        Map<Integer, String> areaMap = iAreaServiceApi.getAreaMap(SecurityUtils.getPkCountry()).getData();
        List<CuMemberGiftVO> memberGiftVOList = new ArrayList<>();
        for (CuMemberGift gift : list) {
            CuMemberGiftVO memberGift = BeanUtil.copyProperties(gift, CuMemberGiftVO.class);
            String str = areaMap.get(gift.getGiftProvince().intValue());
            if (gift.getGiftProvince() != null){
                memberGift.setGiftProvinceVal(areaMap.get(gift.getGiftProvince().intValue()));
            }
            if (gift.getGiftCity() != null){
                memberGift.setGiftCityVal(areaMap.get(gift.getGiftCity().intValue()));
            }
            if (gift.getGiftCounty() != null){
                memberGift.setGiftCountyVal(areaMap.get(gift.getGiftCounty().intValue()));
            }

            memberGiftVOList.add(memberGift);
        }

        return AjaxResult.success(memberGiftVOList);
    }


}
