package com.hzs.system.config.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.domain.system.config.BdAgreement;
import com.hzs.common.domain.system.config.BdRegisterAgree;
import com.hzs.common.domain.system.ext.BdAgreementExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.config.service.IBdAgreementService;
import com.hzs.system.config.service.IBdRegisterAgreeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 协议配置表 前端控制器
 * </p>
 *
 * @author zhangjing
 * @since 2022-09-14
 */
@RestController
@RequestMapping("/api/agreement")
@Slf4j
public class ApiBdAgreementController extends BaseController {

    @Autowired
    private IBdAgreementService agreementService;
    @Autowired
    private IBdRegisterAgreeService registerAgreeService;

    /**
     * @description: 查询专区协议配置
     * @author: zhang jing
     * @date: 2022/8/29 9:56
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @GetMapping("/getAgreement")
    public AjaxResult getAgreement(@RequestBody BdAgreementExt agreementExt) {
        Integer pkCountry=SecurityUtils.getPkCountry();
        agreementExt.setPkCountry(pkCountry);
        BdAgreementExt agreement = agreementService.querySpecialAreaAgreement(agreementExt);
        return AjaxResult.success(agreement);
    }

    /**
     * 查询国家首页强制弹窗
     * @return
     */
    @GetMapping("/get-index-alert")
    public AjaxResult getIndexAlert(){
        Integer pkCountry=SecurityUtils.getPkCountry();
        QueryWrapper<BdAgreement> qw = new QueryWrapper<>();
        qw.eq("IS_INDEX_ALERT", EYesNo.YES.getIntValue());
        qw.eq("PK_COUNTRY",pkCountry);
        BdAgreement agreement = agreementService.getOne(qw);
        return AjaxResult.success(agreement);
    }

    /**
     *  是否弹出 海外国家用户协议  弹窗
     */
    @GetMapping("get-alert-user-agreement")
    public AjaxResult getAlertUserAgreement(){
        QueryWrapper<BdRegisterAgree> qe = new QueryWrapper<>();
        qe.eq("MEMBER_CODE",SecurityUtils.getMemberCode());
        int count = registerAgreeService.count();
        return AjaxResult.success(count);
    }

    /**
     * 海外国家用户协议 弹窗 使用
     * @return
     */
    @PostMapping("use-user-agreement")
    public AjaxResult useUserAgreement(){
        BdRegisterAgree registerAgree = new BdRegisterAgree();
        registerAgree.setMemberCode(SecurityUtils.getMemberCode());
        registerAgree.setAgreeState(EYesNo.YES.getIntValue());
        registerAgree.setPkCountry(SecurityUtils.getPkCountry());
        registerAgree.setPkCreator(SecurityUtils.getUserId());
        registerAgree.setCreationTime(DateUtils.currentDateTime());
        registerAgreeService.save(registerAgree);
        return AjaxResult.success();
    }

}
