<?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\master_worker_credential;


use app\common\model\master_worker_credential\MasterWorkerCredentialImages;
use app\common\logic\BaseLogic;
use app\common\model\master_worker_register\MasterWorkerRegister;
use think\facade\Db;


/**
 * MasterWorkerCredentialImages逻辑
 * Class MasterWorkerCredentialImagesLogic
 * @package app\adminapi\logic
 */
class MasterWorkerCredentialImagesLogic extends BaseLogic
{


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/10/16 16:31
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            MasterWorkerCredentialImages::create([
                'worker_register_id' => $params['worker_register_id'],
                'image' => $params['image'],
                'start_time' => $params['start_time'],
                'end_time' => $params['end_time'],
            ]);

            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/10/16 16:31
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            MasterWorkerCredentialImages::where('worker_register_id', $params['worker_register_id'])->delete();
            $credential_images = [];
            foreach ($params['credential_images'] as $param) {
                /*if(strtotime($param['start_time']) > strtotime($param['end_time'])) {
                    throw new Exception("{$param['image_name']}-时间范围错误");
                }*/
                if(!empty($param['image'])){
                    MasterWorkerCredentialImages::create([
                        'worker_register_id' => $params['worker_register_id'],
                        'image' => $param['image'],
                        'image_name' => $param['image_name'],
                        'start_time' => $param['start_time']??0,
                        'end_time' => $param['end_time']??0,
                    ]);
                    $credential_images[] = $param;
                }
            }
            MasterWorkerRegister::where('id', $params['worker_register_id'])->update(['credential_images'=>json_encode(array_column($credential_images, 'image'))]);
            /*MasterWorkerCredentialImages::where('id', $params['id'])->update([
                'worker_register_id' => $params['worker_register_id'],
                'image' => $params['image'],
                'start_time' => $params['start_time'],
                'end_time' => $params['end_time'],
            ]);*/
            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/10/16 16:31
     */
    public static function delete(array $params): bool
    {
        return MasterWorkerCredentialImages::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2024/10/16 16:31
     */
    public static function detail($params): array
    {
        return MasterWorkerCredentialImages::findOrEmpty($params['id'])->toArray();
    }
}