package com.ruoyi.web.controller.h5;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.CreditUser;
import com.ruoyi.common.core.domain.entity.PolicyMenu;
import com.ruoyi.system.domain.PolicyGuide;
import com.ruoyi.system.domain.UsageRecord;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 入驻商家Controller
 *
 * @author boman
 * @date 2022-06-10
 */
@RestController
@RequestMapping("/system/h5")
public class H5Controller extends BaseController {
    @Autowired
    private ISettledMerchantsService settledMerchantsService;

    @Autowired
    private IPolicyMenuService policyMenuService;

    @Autowired
    private ICreditUserService creditUserService;

    @Autowired
    private IUsageRecordService usageRecordService;

    @Autowired
    private IPolicyGuideService policyGuideService;

    /**
     * 根据政策类别获取入驻商家详细信息
     */
    @GetMapping(value = "/menu/{menuId}")
    public AjaxResult getInfoByMenuId(@PathVariable("menuId") Long menuId) {
        return AjaxResult.success(settledMerchantsService.selectSettledMerchantsByMenuId(menuId));
    }


    /**
     * 查询政策类别列表
     */
    @GetMapping("/policy/list")
    public AjaxResult policyList(PolicyMenu policyMenu) {
        List<PolicyMenu> list = policyMenuService.selectPolicyMenuList(policyMenu);
        return AjaxResult.success(list);
    }


    /**
     * 插入信用人员
     */
    @PostMapping("/insert/creditUser")
    public AjaxResult insertCreditUser(@RequestBody CreditUser creditUser) {
        //先去查询是否存在
        CreditUser creditUserOrder = creditUserService.selectCreditUser(creditUser);
        if (creditUserOrder != null){
            creditUserService.updateCreditUser(creditUser);
            return AjaxResult.success("更新成功");
        }
        return toAjax(creditUserService.insertCreditUser(creditUser));
    }

    /**
     * 获取信用人员详细信息
     */
    @PostMapping(value = "/user/info")
    public AjaxResult getInfo(@RequestBody CreditUser creditUser) {
        if (StringUtils.isBlank(creditUser.getIdCard()) && StringUtils.isBlank(creditUser.getPhoneNum())) {
            return AjaxResult.error("缺少查询参数");
        }
        return AjaxResult.success(creditUserService.selectCreditUser(creditUser));
    }

    /**
     * 查询使用记录列表
     */
    @GetMapping(value = "/record/{creditId}")
    public AjaxResult getRecordList(@PathVariable("creditId") Long creditId) {
        return usageRecordService.selectUsageRecordListByRecordId(creditId);
    }

    /**
     * 查询政策指南列表
     */
    @GetMapping("/guideList")
    public TableDataInfo guideList(PolicyGuide policyGuide) {
        startPage();
        List<PolicyGuide> list = policyGuideService.selectPolicyGuideList(policyGuide);
        return getDataTable(list);
    }

    /**
     * 获取政策指南详细信息
     */
    @GetMapping(value = "/guide/{policyId}")
    public AjaxResult getInfo(@PathVariable("policyId") Long policyId) {
        return AjaxResult.success(policyGuideService.selectPolicyGuideByPolicyId(policyId));
    }
}
