package com.hzs.member.account.controller.api;/**
 * @Description:
 * @Author: yuhui
 * @Time: 2023/7/3 14:41
 * @Classname: ApiCuMemberRelatedAccountsController
 * @PackageName: com.hzs.member.account.controller.api
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.CuMemberRelatedAccountsExt;
import com.hzs.common.domain.member.related.CuMemberRelatedAccounts;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.account.service.ICuMemberRelatedAccountsService;
import com.hzs.member.account.service.impl.CuMemberRelatedAccountsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *@BelongsProject: hzs_cloud
 *@BelongsPackage: com.hzs.member.account.controller.api
 *@Author: yh
 *@CreateTime: 2023-07-03  14:41
 *@Description:  会员关联账户信息 controller
 *@Version: 1.0
 *
 */
@RestController
@RequestMapping("/api/related")
public class ApiCuMemberRelatedAccountsController extends BaseController {

    @Autowired
    private ICuMemberRelatedAccountsService memberRelatedAccountsService;


    /**
     * 关联账户
     * @param memberRelatedAccounts
     * @return
     */
    @PostMapping("related-member")
    public AjaxResult relatedMember(@RequestBody CuMemberRelatedAccounts memberRelatedAccounts){
        memberRelatedAccounts.setPkCountry(SecurityUtils.getPkCountry());
        memberRelatedAccounts.setPkCreator(SecurityUtils.getUserId());
        memberRelatedAccounts.setPkMember(SecurityUtils.getUserId());
        memberRelatedAccounts.setCreationTime(DateUtils.currentDate());
        QueryWrapper<CuMemberRelatedAccounts> qe = new QueryWrapper<>();
        qe.eq("PK_MEMBER",SecurityUtils.getUserId());
        qe.eq("PK_RELATED_MEMBER",memberRelatedAccounts.getPkRelatedMember());
        int count = memberRelatedAccountsService.count(qe);
        if (count > 0){
            return AjaxResult.error("当前账户已关联");
        }
        memberRelatedAccountsService.save(memberRelatedAccounts);
        return AjaxResult.success();
    }


    /**
     * 关联账户列表
     * @param memberCode
     * @param memberName
     * @return
     */
    @GetMapping("list")
    public TableDataInfo list(String memberCode,String memberName){
        startPage();
        List<CuMemberRelatedAccountsExt> list = memberRelatedAccountsService.queryList(memberCode,memberName,SecurityUtils.getUserId());
        return getDataTable(list);
    }
}
