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

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.utils.DateUtils;
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.ext.CuHonorWallExt;
import com.hzs.common.domain.member.ext.CuMemberExt;
import com.hzs.common.domain.member.ext.CuMemberHonorWallExt;
import com.hzs.common.domain.member.honor.CuHonorWall;
import com.hzs.common.domain.member.honor.CuMemberHonorWall;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.member.base.service.ICuHonorWallService;
import com.hzs.member.base.service.ICuMemberHonorWallService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @BelongsProject: hzs_cloud
 * @BelongsPackage: com.hzs.member.base.controller.api
 * @Author: yh
 * @CreateTime: 2023-04-26  20:30
 * @Description:
 * @Version: 1.0
 */
@RequestMapping("/api/member")
@RestController
public class ApiHonorWallController extends BaseController {

    @Autowired
    private ICuMemberHonorWallService memberHonorWallService;
    @Autowired
    private ICuHonorWallService honorWallService;

    @DubboReference
    IMemberServiceApi memberServiceApi;

    /**
     * 查询会员荣誉墙信息
     *
     * @return
     */
    @GetMapping("member-honor-wall")
    public AjaxResult memberHonorWall() {
        List<CuMemberHonorWallExt> resultList = memberHonorWallService.getByMemberHonorWall(SecurityUtils.getUserId());
        return AjaxResult.success(resultList);
    }


    /**
     * 查询会员荣誉墙
     *
     * @return
     */
    @GetMapping("member-honor-wall-all")
    public TableDataInfo memberHonorWallAll() {
        Integer pkCountry = SecurityUtils.getPkCountry();
        Long pkMember = SecurityUtils.getUserId();
        startPage();
        List<CuHonorWallExt> resultList = honorWallService.queryAll(pkCountry, pkMember);
        for (CuHonorWallExt cuHonorWallExt : resultList) {
            if (cuHonorWallExt.getPkMember() == null) {
                // 未拥有
                cuHonorWallExt.setIsHave(EYesNo.NO.getIntValue());
            } else {
                // 已拥有
                cuHonorWallExt.setIsHave(EYesNo.YES.getIntValue());
            }
        }
        return getDataTable(resultList);
    }

    /**
     * 设置会员当前荣誉墙信息（测试接口）
     */
    @GetMapping("set-member-honor-wall-info")
    public AjaxResult setMemberHonorWallInfo() {
        R<List<CuMemberExt>> memberExtDto = memberServiceApi.findAll();
        for (CuMemberExt member : memberExtDto.getData()) {
            Integer pkAwards = member.getPkAwards();
            Integer pkSettleGrade = member.getPkSettleGrade();
            QueryWrapper<CuHonorWall> qw = new QueryWrapper<>();
            qw.eq("TYPE", 2);
            qw.eq("VALUE", pkAwards);
            List<CuHonorWall> honorWallAwardsList = honorWallService.list(qw);
            QueryWrapper<CuHonorWall> qe = new QueryWrapper<>();
            qe.eq("TYPE", 1);
            qe.eq("VALUE", pkSettleGrade);
            List<CuHonorWall> honorWallList = honorWallService.list(qe);
            for (CuHonorWall honorWall : honorWallAwardsList) {
                CuMemberHonorWall memberHonorWall = new CuMemberHonorWall();
                memberHonorWall.setPkMember(member.getPkId());
                memberHonorWall.setPkHonorWall(honorWall.getPkId());
                memberHonorWall.setSettleMonth(DateUtil.format(new Date(), "yyyyMM"));
                memberHonorWall.setCreationTime(DateUtils.currentDate());
                memberHonorWall.setPkCountry(SecurityUtils.getPkCountry());
                memberHonorWall.setPkCreator(SecurityUtils.getUserId());
                memberHonorWallService.saveCuMemberHonorWall(memberHonorWall);
            }
            for (CuHonorWall honorWall : honorWallList) {
                CuMemberHonorWall memberHonorWall = new CuMemberHonorWall();
                memberHonorWall.setPkMember(member.getPkId());
                memberHonorWall.setPkHonorWall(honorWall.getPkId());
                memberHonorWall.setSettleMonth(DateUtil.format(new Date(), "yyyyMM"));
                memberHonorWall.setCreationTime(DateUtils.currentDate());
                memberHonorWall.setPkCountry(SecurityUtils.getPkCountry());
                memberHonorWall.setPkCreator(SecurityUtils.getUserId());
                memberHonorWallService.saveCuMemberHonorWall(memberHonorWall);
            }
        }
        return AjaxResult.success();
    }
}
