package com.pj.api.h5;


import com.pj.api.bo.InOutRecordBO;
import com.pj.api.service.ApiService;
import com.pj.constants.UserTypeEnum;
import com.pj.current.satoken.StpUserUtil;
import com.pj.project.tb_business.TbBusinessService;
import com.pj.project.tb_costomer.TbCostomer;
import com.pj.project.tb_costomer.TbCostomerService;
import com.pj.project4sp.admin4login.SpAccAdminService;
import com.pj.utils.sg.AjaxJson;
import com.pj.utils.sg.NbUtil;
import com.pj.utils.so.SoMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@RequestMapping(value = "/api")
@RestController
@Slf4j
public class ApiController {

    @Resource
    ApiService apiService;
    @Resource
    SpAccAdminService spAccAdminService;
    @Resource
    TbCostomerService tbCostomerService;
    @Resource
    TbBusinessService tbBusinessService;

    /** 账号、密码登录  */
    @RequestMapping("doLogin")
    AjaxJson doLogin(String key, String password) {
        // 1、验证参数
        if(NbUtil.isOneNull(key, password)) {
            return AjaxJson.getError("请提供key与password参数");
        }
        return spAccAdminService.doLogin(key, password);
    }

    @PostMapping(value = "register")
    public AjaxJson register(TbCostomer costomer){
        tbCostomerService.register(costomer);
        return AjaxJson.getSuccess();
    }

    @RequestMapping(value = "getInOutRecord")
    public AjaxJson getInOutRecord(){
        SoMap so = SoMap.getRequestSoMap();
        String currentCustomerId = StpUserUtil.getCustomerId();
        if (!currentCustomerId.equals(UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
            so.put("customerId",currentCustomerId);
        }
        List<InOutRecordBO> recordList = apiService.getInOutRecord(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), recordList);
    }

    @RequestMapping(value = "getCustomerList")
    public AjaxJson getCustomerList(){
        SoMap so = SoMap.getRequestSoMap();
        String currentCustomerId = StpUserUtil.getCustomerId();
        if (!currentCustomerId.equals(UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
            so.put("id",currentCustomerId);
        }
        List<TbCostomer> list = apiService.getCustomerList(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);
    }

    @PostMapping(value = "confirmCustomer")
    public AjaxJson confirmCustomer(Long customerId, String judgeContent){
        apiService.confirmCustomer(customerId, judgeContent);
        return AjaxJson.getSuccess();
    }
    @PostMapping(value = "confirm")
    public AjaxJson confirmPay(){
        SoMap so = SoMap.getRequestSoMap();
      String id=  so.getString("id");
      tbBusinessService.confirm(Collections.singletonList(id));
        return AjaxJson.getSuccess();
    }


}
