<?php
namespace app\finance\model;

use think\Model;
use think\facade\Db;

class PriceAdjust extends Model
{
    public function datalist($param = [], $where = [], $whereOr = [])
    {
        $rows  = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
        $order = empty($param['order']) ? 'id desc' : $param['order'];
        try {
            $list = self::where($where)
                ->where(function ($query) use ($whereOr) {
                    if (!empty($whereOr)) {
                        $query->whereOr($whereOr);
                    }
                })
                ->order($order)
                ->paginate(['list_rows' => $rows])
                ->each(function ($item) {
                    $item->check_status_str = check_status_name($item->check_status);
                    $item->admin_name       = Db::name('Admin')->where('id', $item->admin_id)->value('name');
                    $item->department       = Db::name('Department')->where('id', $item->did)->value('title');
                    $item->create_time_str  = to_date($item->create_time);
                });
            return $list;
        } catch (\Exception $e) {
            return ['code' => 1, 'data' => [], 'msg' => $e->getMessage()];
        }
    }

    public function add($param)
    {
        try {
            $param['create_time'] = time();
            $insertId = self::strict(false)->field(true)->insertGetId($param);
            add_log('add', $insertId, $param, '调价申请');
        } catch (\Exception $e) {
            return to_assign(1, '操作失败，原因：' . $e->getMessage());
        }
        return to_assign(0, '操作成功', ['return_id' => $insertId]);
    }

    public function edit($param)
    {
        try {
            $param['update_time'] = time();
            self::where('id', $param['id'])->strict(false)->field(true)->update($param);
            add_log('edit', $param['id'], $param, '调价申请');
        } catch (\Exception $e) {
            return to_assign(1, '操作失败，原因：' . $e->getMessage());
        }
        return to_assign(0, '操作成功', ['return_id' => $param['id']]);
    }

    public function getById($id)
    {
        $info = self::find($id);
        if (empty($info)) {
            return [];
        }
        $info['admin_name']         = Db::name('Admin')->where('id', $info['admin_id'])->value('name');
        $info['department']         = Db::name('Department')->where('id', $info['did'])->value('title');
        $info['trade_scene_name'] = Db::name('PriceAdjustCate')->where(['type' => 1, 'value' => $info['trade_scene'], 'delete_time' => 0])->value('title') ?? '';
        $info['card_bin_name']    = Db::name('PriceAdjustCate')->where(['type' => 2, 'value' => $info['card_bin'], 'delete_time' => 0])->value('title') ?? '';
        $info['card_type_name']   = Db::name('PriceAdjustCate')->where(['type' => 3, 'value' => $info['card_type'], 'delete_time' => 0])->value('title') ?? '';
        if (!empty($info['file_ids'])) {
            $info['file_array'] = Db::name('File')->where('id', 'in', $info['file_ids'])->select();
        }
        return $info;
    }

    public function delById($id, $type = 0)
    {
        try {
            if ($type == 0) {
                self::where('id', $id)->update(['delete_time' => time()]);
            } else {
                self::destroy($id);
            }
            add_log('delete', $id);
        } catch (\Exception $e) {
            return to_assign(1, '操作失败，原因：' . $e->getMessage());
        }
        return to_assign();
    }
}
