package com.xhpc.user.controller;

import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.common.util.UserTypeUtil;
import com.xhpc.user.service.IXhpcCommonService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 公共的内
 * @author yuyang
 * @date 2021/12/29 10:34
 */
@RestController
@RequestMapping("/common")
public class XhpcCommonController extends BaseController {

    @Autowired
    private IXhpcCommonService xhpcCommonService;

    /**
     * 根据手机号（账号），用户id和类型 获取用户信息
     *
     * @param phone
     * @param userId
     * @param userType
     * @param serialNumber 桩号
     * @return
     */
    @GetMapping(value = "/getUser")
    public R getMechanism(String phone, Long userId, Integer userType, String serialNumber, String tenantId) {

        if (phone != null || "".equals(phone)) {
            if (!UserTypeUtil.COMMUNIT.equals(phone.substring(0, 1)) && !UserTypeUtil.CUSTOMERS.equals(phone.substring(0, 1)) && UserTypeUtil.USER_TYPE ==userType) {
                //C端用户
                return R.ok(xhpcCommonService.getLandUser(phone, null, UserTypeUtil.USER_TYPE, serialNumber, tenantId));
            } else if (UserTypeUtil.COMMUNIT.equals(phone.substring(0, 1))) {
                //社区用户
                return R.ok(xhpcCommonService.getLandUser(phone, null, UserTypeUtil.COMMUNIT_TYPE, serialNumber, tenantId));
            } else if (UserTypeUtil.CUSTOMERS.equals(phone.substring(0, 1))) {
                //大客户用户
                return R.ok(xhpcCommonService.getLandUser(phone, null, UserTypeUtil.CUSTOMERS_TYPE, serialNumber, tenantId));
            }else if(UserTypeUtil.INTERNET_TYPE ==userType){
                return R.ok(xhpcCommonService.getLandUser(phone, null, UserTypeUtil.INTERNET_TYPE, serialNumber, tenantId));
            }else if(UserTypeUtil.MOTORCADE_USER.equals(phone.substring(0, 1))){
                //车队个人
                return R.ok(xhpcCommonService.getLandUser(phone, null, UserTypeUtil.MOTORCADE_USER_TYPE, serialNumber, tenantId));
            }else  if(UserTypeUtil.MOTORCADE_PERSON_IN_CHARGE.equals(phone.substring(0, 1))){
                //车队总负责人
                return R.ok(xhpcCommonService.getLandUser(phone, null, UserTypeUtil.MOTORCADE_TYPE, serialNumber, tenantId));
            }
        }else{
            return R.ok(xhpcCommonService.getLandUser(null, userId, userType, serialNumber, tenantId));
        }
        return R.fail();
    }

    /**
     * 根据用户类型，用户id，充值记录，增加流水
     * @param money          充值金额
     * @param userId          充值用户
     * @param userType        用户类型
     * @param tenantId        租户id
     * @param type            充值渠道（1微信 2支付宝  3平台)
     * @param status           1 充值 2退款
     * @param remark          备注
     * @return
     */
    @GetMapping(value = "/insertUserBalance")
    public AjaxResult insertUserBalance(BigDecimal money, Long userId, Integer userType, String tenantId, Integer type, Integer status,String remark,Long moneyOrderId) {

        return xhpcCommonService.insertUserBalance(money,userId,userType,tenantId,type,status,remark,moneyOrderId);
    }

    /**
     * 获取该机构的折扣比例
     * @param userId          充值用户
     * @param userType        用户类型
     * @param tenantId        租户id
     * @return
     */
    @GetMapping(value = "/getMechanismDiscount")
    public AjaxResult getMechanismDiscount(Long userId, Integer userType, String tenantId, Long chargingStationId) {

        return xhpcCommonService.getMechanismDiscount(userId,userType,tenantId,chargingStationId);
    }

    /**
     * 机构人员统计
     */
    @GetMapping(value = "/getPersonnelStatistics")
    public TableDataInfo getPersonnelStatistics(String tenantId,Long terminalId,Long operatorId, Long chargingStationId, Long chargingPileId, Integer type, String name , String phone, String account,String startTime,String endTime,String mark) {
        List<Map<String, Object>> list = xhpcCommonService.getPersonnelStatistics(tenantId,terminalId,operatorId,chargingStationId,chargingPileId,type,name,phone,account,startTime,endTime,mark);
        return getDataTable(list);
    }

    /**
     * 机构人员统计
     */
    @PostMapping(value = "/getPersonnelStatisticsExport")
    public void getPersonnelStatisticsExport(HttpServletResponse response,String tenantId,Long terminalId, Long operatorId, Long chargingStationId, Long chargingPileId, Integer type, String name , String phone, String account,String startTime,String endTime,String mark)throws IOException {
         xhpcCommonService.getPersonnelStatisticsExport(response,tenantId,terminalId,operatorId,chargingStationId,chargingPileId,type,name,phone,account,startTime,endTime,mark);
    }




}
