<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\logic\animal_info;


use app\common\logic\RemoteRequestLogic;
use app\common\model\animal_info\AnimalInfo;
use app\common\logic\BaseLogic;
use think\facade\Db;


/**
 * AnimalInfo逻辑
 * Class AnimalInfoLogic
 * @package app\adminapi\logic\animal_info
 */
class AnimalInfoLogic extends BaseLogic
{


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/01/10 15:53
     */
    public static function add(array $params): bool
    {
        $repeat = AnimalInfo::where('sn', $params['sn'])->count();
        if ($repeat > 0) {
            self::setError('耳号已使用');
            return false;
        }
        Db::startTrans();
        try {
            AnimalInfo::create([
                'fence_house_id' => $params['fence_house_id'],
                'sn' => $params['sn'],
                'animal_type' => $params['animal_type'],
                'brand' => $params['brand'],
                'physi_stage' => $params['physi_stage'],
                'gender' => $params['gender'],
                'blood_purity' => $params['blood_purity'],
                'animal_source' => $params['animal_source'],
                'current_estimation' => $params['current_estimation'],
                'algebra' => $params['algebra'],
                'birth' => $params['birth'],
                'entry_date' => $params['entry_date'],
                'birth_estimation' => $params['birth_estimation'],
                'health_condition' => $params['health_condition'],
                'pic' => $params['pic'],
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/01/10 15:53
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            AnimalInfo::where('id', $params['id'])->update([
                'fence_house_id' => $params['fence_house_id'],
                'sn' => $params['sn'],
                'animal_type' => $params['animal_type'],
                'brand' => $params['brand'],
                'physi_stage' => $params['physi_stage'],
                'gender' => $params['gender'],
                'blood_purity' => $params['blood_purity'],
                'animal_source' => $params['animal_source'],
                'current_estimation' => $params['current_estimation'],
                'algebra' => $params['algebra'],
                'birth' => $params['birth'],
                'entry_date' => $params['entry_date'],
                'birth_estimation' => $params['birth_estimation'],
                'health_condition' => $params['health_condition'],
                'pic' => $params['pic'],
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 删除
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/01/10 15:53
     */
    public static function delete(array $params): bool
    {
        return AnimalInfo::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2024/01/10 15:53
     */
    public static function detail($params): array
    {
        if (isset($params['sn'])) {
            $data = AnimalInfo::with(['fenceHouseAttr'])->where('sn', $params['sn'])->findOrEmpty()->toArray();
        } else {
            $data = AnimalInfo::with(['fenceHouseAttr'])->findOrEmpty($params['id'])->toArray();
        }
        // 查动物体温
        $res = RemoteRequestLogic::requestAnimalTemperature($data['sn']);
        if (!empty($res['values'])) {
            $temperature = $res['values'][1][0];
        } else {
            $temperature = '暂无数据';
        }
        $data['temperature'] = $temperature;
        return $data;

    }
}