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


import cn.hutool.core.bean.BeanUtil;
import com.hzs.common.core.constant.msg.MemberMsgConstants;
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.base.CuMember;
import com.hzs.common.domain.member.ext.CuMemberExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.member.swing.param.CuMemberSwingNetParam;
import com.hzs.member.swing.service.ICuMemberSwingNetService;
import com.hzs.member.swing.vo.CuMemberRetVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 会员网体甩网配置 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2023-08-30
 */
@RestController
@RequestMapping("/manage/swing")
public class CuMemberSwingNetController extends BaseController {

    @Autowired
    private ICuMemberSwingNetService cuMemberSwingNetService;

    @Autowired
    private ICuMemberService cuMemberService;

    /*
     * @description: 保存甩网配置
     * @author: sui q
     * @date: 2023/8/30 14:02
     * @param: null null
     **/
    @PutMapping("/save-net")
    public AjaxResult saveCuMemberSwingNet(@RequestBody CuMemberSwingNetParam cuMemberSwingNetParam) {
        cuMemberSwingNetService.saveCuMemberSingNet(cuMemberSwingNetParam, SecurityUtils.getPkCountry(), SecurityUtils.getUserId());
        return AjaxResult.success();
    }

    /*
     * @description: 查询甩网数据
     * @author: sui q
     * @date: 2023/8/30 16:52
     * @param: null null
     **/
    @PostMapping("/query-net")
    public TableDataInfo queryCuMemberSwingNet(@RequestBody CuMemberSwingNetParam cuMemberSwingNetParam) {
        startPage();
        return getDataTable(cuMemberSwingNetService.queryCuMemberSwingNetExt(cuMemberSwingNetParam));
    }

    @PostMapping("/query-member")
    public AjaxResult queryMember(@RequestBody CuMemberSwingNetParam cuMemberSwingNetParam){
        if(StringUtils.isEmpty(cuMemberSwingNetParam.getMemberCode())){
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.MEMBER_NOT_EXITS));
        }
        CuMember cuMember = cuMemberService.getMember(cuMemberSwingNetParam.getMemberCode());
        if(cuMember == null){
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.MEMBER_NOT_EXITS));
        }
        return AjaxResult.success(BeanUtil.copyProperties(cuMember, CuMemberRetVO.class));
    }

    @PostMapping("/query-member-parent")
    public AjaxResult queryMemberParent(@RequestBody CuMemberSwingNetParam cuMemberSwingNetParam){
        if(StringUtils.isEmpty(cuMemberSwingNetParam.getMemberCode())){
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.MEMBER_NOT_EXITS));
        }
        CuMember cuMember = cuMemberService.getMember(cuMemberSwingNetParam.getMemberCode());
        if(cuMember == null){
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.MEMBER_NOT_EXITS));
        }
        List<CuMemberExt> cuMemberExtList = cuMemberService.queryPlaceSponsorOnUmbrella(cuMember.getPkId(), null, null);
        return AjaxResult.success(cuMemberExtList);
    }
}
