<?php

namespace app\admin\logic;

use app\model\CommonModel;
use app\model\CompanyModel;
use think\exception\ValidateException;
use think\response\Json;

class CompanyLogic extends BaseLogic
{
    //获取企业列表
    public static function list(array $data = []): Json
    {
        $db = CompanyModel::where('is_del', CommonModel::$del_normal);
        if ($data['status'] != '') $db->where('status', $data['status']);
        if ($data['title']) $db->whereLike('title', '%' . $data['title'] . '%');
        if ($data['contacts'] !== '') $db->whereLike('contacts', '%' . $data['contacts'] . '%');
        if ($data['mobile'] != '') $db->whereLike('mobile', '%' . $data['mobile'] . '%');

        $count = $db->count('id');

        $list = $db
            ->field('id,title,contacts,mobile,remark,status,addtime')
            ->page($data['page'], $data['size'])
            ->order(['addtime' => 'desc', 'id' => 'desc'])
            ->select()
            ->toArray();

        return json_show(CommonModel::$success, '获取成功', ['list' => $list, 'count' => $count]);
    }

    //获取企业列表
    public static function all(string $keyword = ''): Json
    {
        $db = CompanyModel::where('is_del', CommonModel::$del_normal);
        if ($keyword != '') $db->whereLike('title', '%' . $keyword . '%');

        $list = $db
            ->field('id,title,status')
            ->order(['addtime' => 'desc', 'id' => 'desc'])
            ->select()
            ->toArray();

        return json_show(CommonModel::$success, '获取成功', $list);
    }

    //添加企业
    public static function add(array $data = []): Json
    {
        $rs = CompanyModel::field('id')
            ->where(['is_del' => CommonModel::$del_normal, 'title' => $data['title']])
            ->findOrEmpty()
            ->isEmpty();

        if (!$rs) throw new ValidateException('该企业名称已存在');

        $data = array_merge($data, [
            'is_del' => CommonModel::$del_normal,
            'status' => CommonModel::$status_normal,
            'createrid' => self::$uid,
            'creater' => self::$uname,
            'addtime' => date('Y-m-d H:i:s'),
            'updaterid' => self::$uid,
            'updater' => self::$uname,
            'updatetime' => date('Y-m-d H:i:s'),
        ]);

        $res = CompanyModel::create($data)->save();

        return $res ? json_show(CommonModel::$success, '企业新建成功') : json_show(CommonModel::$error_param, '企业新建失败');
    }

    //读取企业详情
    public static function read(int $id = 0): Json
    {
        $rs = CompanyModel::field(true)
            ->where(['id' => $id, 'is_del' => CommonModel::$del_normal])
            ->findOrEmpty()
            ->toArray();
        return json_show(CommonModel::$success, '获取企业详情成功', $rs);
    }

    //编辑企业
    public static function edit(array $data = []): Json
    {
        $rs = CompanyModel::field('id,title')
            ->where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
            ->findOrEmpty();

        if ($rs->isEmpty()) throw new ValidateException('该企业不存在');

        //修改了企业名称,检查企业名称是否重复
        if ($rs->title != $data['title']) {
            $temp = CompanyModel::field('id')
                ->where(['is_del' => CommonModel::$del_normal, 'title' => $data['title']])
                ->findOrEmpty()
                ->isEmpty();
            if (!$temp) throw new ValidateException('该企业名称重复');
        }

        $data = array_merge($data, [
            'updaterid' => self::$uid,
            'updater' => self::$uname,
            "updatetime" => date("Y-m-d H:i:s"),
        ]);

        $res = CompanyModel::where(['id' => $data['id']])->save($data);

        return $res ? json_show(CommonModel::$success, '企业编辑成功') : json_show(CommonModel::$error_param, '企业编辑失败');

    }

    //企业启禁用
    public static function status(array $data = []): Json

    {
        $where = [
            ['id', '=', $data['id']],
            ['is_del', '=', CommonModel::$del_normal],
            ['status', '<>', $data['status']],
        ];
        $rs = CompanyModel::field('id,status')
            ->where($where)
            ->findOrEmpty()
            ->isEmpty();

        if ($rs) throw new ValidateException('该企业不存在或重复操作');

        $data = array_merge($data, [
            'updaterid' => self::$uid,
            'updater' => self::$uname,
            "updatetime" => date("Y-m-d H:i:s"),
        ]);

        $res = CompanyModel::where($where)->save($data);

        return $res ? json_show(CommonModel::$success, '更新成功') : json_show(CommonModel::$error_param, '更新失败');

    }

    //删除企业
    public static function delete(int $id = 0): Json
    {
        $rs = CompanyModel::where(['id' => $id, 'is_del' => CommonModel::$del_normal])
            ->save(['is_del' => CommonModel::$del_deleted]);

        return $rs ? json_show(CommonModel::$success, '企业删除成功') : json_show(CommonModel::$error_param, '企业不存在或重复删除');
    }

}