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


use app\common\model\DataReception;
use app\common\logic\BaseLogic;
use think\facade\Db;


/**
 * DataReception逻辑
 * Class DataReceptionLogic
 * @package app\adminapi\logic
 */
class DataReceptionLogic extends BaseLogic
{


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/02/23 09:21
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            DataReception::create([
                'dataid' => generate_sn(DataReception::class, 'dataid'),
                'num' => generate_sn(DataReception::class, 'num'),
                'project_id' => $params['project_id'],
                'apptime' => $params['apptime'],
                'person' => $params['person'],
                'number' => $params['number'],
                'position' => $params['position'],
                'tips' => $params['tips'],
                'bidding_file' => json_encode($params['bidding_file']),
                'zbkzj_file' => json_encode($params['zbkzj_file']),
                'gczjht_file' => json_encode($params['gczjht_file']),
                'xmbgqzzl_file' => json_encode($params['xmbgqzzl_file']),
                'ssgsysjs_file' => json_encode($params['ssgsysjs_file']),
                'wlhj_file' => json_encode($params['wlhj_file']),
                'other_file' => json_encode($params['other_file']),
            ]);

            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/02/23 09:21
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            DataReception::where('id', $params['id'])->update([
                'project_id' => $params['project_id'],
                'apptime' => $params['apptime'],
                'person' => $params['person'],
                'number' => $params['number'],
                'position' => $params['position'],
                'tips' => $params['tips'],
                'bidding_file' => json_encode($params['bidding_file']),
                'zbkzj_file' => json_encode($params['zbkzj_file']),
                'gczjht_file' => json_encode($params['gczjht_file']),
                'xmbgqzzl_file' => json_encode($params['xmbgqzzl_file']),
                'ssgsysjs_file' => json_encode($params['ssgsysjs_file']),
                'wlhj_file' => json_encode($params['wlhj_file']),
                'other_file' => json_encode($params['other_file']),
            ]);

            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/02/23 09:21
     */
    public static function delete(array $params): bool
    {
        return DataReception::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2024/02/23 09:21
     */
    public static function detail($params): array
    {
        $data = DataReception::with(['projectInfo'])->findOrEmpty($params['id'])->toArray();
        $data['bidding_file'] = json_decode($data['bidding_file'], true);
        $data['zbkzj_file'] = json_decode($data['zbkzj_file'], true);
        $data['gczjht_file'] = json_decode($data['gczjht_file'], true);
        $data['xmbgqzzl_file'] = json_decode($data['xmbgqzzl_file'], true);
        $data['ssgsysjs_file'] = json_decode($data['ssgsysjs_file'], true);
        $data['wlhj_file'] = json_decode($data['wlhj_file'], true);
        $data['other_file'] = json_decode($data['other_file'], true);
        return $data;
    }
}