<?php

namespace app\common\dao\user;

use app\common\dao\BaseDao;
use app\common\enum\user\UserSignScoreEnum;
use app\common\model\BaseModel;
use app\common\model\user\UserSignScore;
use app\entity\CommonEntity;
use app\entity\data\user\UserSignScoreEntity;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Log;

class UserSignScoreDao extends BaseDao
{
    /**
     * @return BaseModel
     */
    protected function getModel(): string
    {
        return UserSignScore::class;
    }

    /**
     * @param array $idList
     * @return CommonEntity[]
     */
    public function getUserSignScoreEntityListByIdList(array $idList): array
    {
        $entityList = [];
        try {
            /** @var UserSignScore $model */
            $model = $this->getModel();
            $dataRes = $model::getDB()
                ->whereIn('id', $idList)
                ->select()
                ->toArray();
            if (!empty($dataRes)) {
                $entityList = array_map(function ($data) {
                    return UserSignScoreEntity::newInstance($data);
                }, $dataRes);
            }
        } catch (\Exception $e) {

        }
        return $entityList;
    }

    /**
     * @param string $orderType
     * @param array $orderIdList
     * @return UserSignScoreEntity[]
     */
    public function getUserSignScoreEntityListByOrderIdListAndPending(string $orderType, array $orderIdList): array
    {
        $entityList = [];
        try {
            $dataList = UserSignScore::getDB()
                ->where('order_type', '=', $orderType)
                ->whereIn('order_id', $orderIdList)
                ->where('status', '=', UserSignScoreEnum::STATUS['Pending']['code'])
                ->select()
                ->toArray();
            if (!empty($dataList)) {
                $entityList = array_map(function ($data) {
                    return UserSignScoreEntity::newInstance($data);
                }, $dataList);
            }
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {

        }
        return $entityList;
    }

    /**
     * 批量更新
     * @param $dataList
     * @return array
     */
    public function saveAll($dataList): array
    {
        try {
            return $this->getModel()::getInstance()->allowField(array_keys(reset($dataList)))->saveAll($dataList)->toArray();
        } catch (\Exception $e) {
            return [];
        }
    }
    /**
     * 根据order_id与uid查找奖励积分值（status=1, source_type=3, pm=1）
     */
    public function getRewardScoreFromUidOrderId($orderId, $uid)
    {
        try {
            return $this::getModel()::getDB()
                ->where('order_id', '=', $orderId)
                ->where('status', '=', 1)
                ->where('source_type', '=', 3)
                ->where('uid', '=', $uid)
                ->where('pm', '=', 1)
                ->value('reward_socre');
        } catch (\Exception $e) {
            return [];
        }
    }
    /**
     * 根据order_id与uid查找支付积分值（status=1, pm=2）
     */
    public function getPayScoreFromUidOrderId($orderId, $uid)
    {
        try {
            return $this::getModel()::getDB()
                ->where('order_id', '=', $orderId)
                ->where('status', '=', 1)
                ->where('uid', '=', $uid)
                ->where('pm', '=', 2)
                ->value('reward_socre');
        } catch (\Exception $e) {
            return [];
        }
    }
    /**
     * 根据order_id与uid查找奖励积分值（status=1）
     */
    public function getRewardScoreFromStatusUidOrderId($orderId, $uid)
    {
        try {
            return $this::getModel()::getDB()
                ->where('order_id', '=', $orderId)
                ->where('uid', '=', $uid)
                ->where('status', '=', 1)
                ->value('reward_socre');
        } catch (\Exception $e) {
            return [];
        }
    }
    /**
     * 根据order_id查找扫码推荐人uid（mark like '%扫码%', status=1）
     */
    public function getUidFromMarkLikeStatusOrderId($orderId)
    {
        try {
            return $this::getModel()::getDB()
                ->where('order_id', '=', $orderId)
                ->whereLike('mark', '%扫码%')
                ->where('status', '=', 1)
                ->value('uid');
        } catch (\Exception $e) {
            return [];
        }
    }
    /**
     * 根据上面getUidFromMarkLikeStatusOrderId方法查处的Uid来确定score
     */
    public function getScoreFromUidOrderId($orderId, $smSpreadUid)
    {
        try {
            return $this::getModel()::getDB()
                ->where('order_id', '=', $orderId)
                ->where('uid', '=', $smSpreadUid)
                ->where('status', '=', 1)
                ->value('reward_socre');
        } catch (\Exception $e) {
            return [];
        }
    }
}
