<?php

namespace app\controller\api\v1;

use app\controller\api\Base;
use app\model\Account;
use app\model\AccountDimission;
use app\model\AccountStar;
use app\model\AccountWorksite;
use app\model\CheckLog;
use app\model\ClockLog;
use app\model\OvertimeLog;
use app\model\Position;
use app\model\Worksite;
use app\service\Math;
use Exception;
use Lcobucci\Clock\Clock;
use think\Collection;
use think\facade\Db;
use think\response\Json;

/**
 * 负责人相关
 *
 * Class Manager
 * @package app\controller\api\v1
 */
class Manager extends Base
{
    protected $noNeedLogin = [];

    /**
     * 查看审核详情
     *
     * @return Json
     */
    public function checkDetail(): Json
    {
        try {
            $accountId = $this->request->user['user_id'] ?? 0;
            $id        = input('id/d');//待审核记录ID

            if (!$account = Account::findById($accountId, ['id, role'])) {
                return $this->json(6001, '请先登录');
            }

            if ($account['role'] <= Account::COMMON_ON) {
                // 工地负责人才能查看
                return $this->json(4003, '无查看权限');
            }

            if (!$item = CheckLog::findById($id)) {
                return $this->json(4004, '记录不存在');
            }

            $item = arrayNullToString($item->toArray());

            $fields = Account::needCheckFields();
            array_push($fields, 'id', 'role', 'work_at', 'worksite_id');

            $user = Account::findById($item['account_id'], $fields)->toArray();
            $user = arrayNullToString($user);

            $positionList          = Position::column('name', 'id');
            $item['position_name'] = $positionList[$item['position']] ?? '';
            $user['position_name'] = $positionList[$user['position']] ?? '';

            // user角色为工人时,需要判断当前人是否是该工地的负责人
            if (!empty($user) && $user['role'] == Account::COMMON_ON) {
                $managerIds = AccountWorksite::where('worksite_id', $user['worksite_id'])->column('account_id');
                if (!in_array($accountId, $managerIds)) {
                    return $this->json(4003, '无查看权限2');
                }
            }

            $item['account'] = $user;

            return $this->json(0, 'success', $item);
        } catch (Exception $e) {
            return $this->json(4000, '没有相关的用户记录'.$e->getMessage());
        }
    }

    // 审核列表(员工资料提交的审核)
    public function checkList(): Json
    {
        $page    = input('page/d', 1);
        $size    = input('size/d', 20);
        $keyword = input('keyword/s');
        $status  = input('status/d', 0);//状态 0=待审核 1=已审核(包含1通过 -1不通过)

        $accountId = $this->request->user['user_id'] ?? 0;

        if (!$account = Account::findById($accountId, ['id, role'])) {
            return $this->json(6001, '请先登录');
        }

        if ($account['role'] <= Account::COMMON_ON) {
            return $this->json(4003, '无查看权限');
        }

        $where = [];

        if (!empty($keyword)) {
            $where[] = ['cl.real_name|cl.mobile', 'like', '%'.$keyword.'%'];
        }

        if ($status == 0) {
            $where[] = ['cl.status', '=', 0];
        } else {
            $where[] = ['cl.status', 'in', [1, -1]];
        }

        // 负责工地
        $worksiteIds = AccountWorksite::where('account_id', $accountId)->column('worksite_id');
        $where[]     = ['cl.worksite_id', 'in', $worksiteIds];

        $query = CheckLog::alias('cl')
            ->leftJoin('account a', 'a.id = cl.account_id')
            ->leftJoin('position p', 'p.id = cl.position')
            ->field('cl.id,cl.real_name,cl.status,cl.created_at,p.name as position_name')
            ->where($where);

        $total = $query->count();

        $res = [
            'total'   => $total,
            'current' => $page ?: 1,
            'size'    => $size ?: 20,
            'list'    => new Collection(),
        ];

        if ($total > 0) {
            $res['list'] = $query->page($page, $size)->order('cl.id', 'desc')->select();
            $res['list']->each(function ($item) {
                $item->created_at = date('Y年m月d日 H:i:s', strtotime($item->created_at));
            });
            $res['list'] = arrayNullToString($res['list']->toArray());
        }

        return $this->json(0, 'success', $res);
    }

    /**
     * 审核资料(工人信息注册或二次修改审核)
     *
     * @return Json
     */
    public function checkInfo(): Json
    {
        try {
            $accountId = $this->request->user['user_id'] ?? 0;
            $type      = input('type/d', 1);//类型 1=通过 0=不通过
            $id        = input('id/d');//待审核记录ID
            $reason    = input('reason/s');//不通过原因

            if (!in_array($type, [0, 1])) {
                return $this->json(4001, '审核参数错误');
            }

            if (!$account = Account::findById($accountId, ['id, role'])) {
                return $this->json(6001, '请先登录');
            }

            if ($account['role'] <= Account::COMMON_ON) {
                // 工地负责人才能操作
                return $this->json(4003, '无此权限');
            }

            if (!$item = CheckLog::findById($id)) {
                return $this->json(4004, '记录不存在');
            }

            if ($item['status'] != CheckLog::COMMON_OFF) {
                return $this->json(4002, '状态错误');
            }

            $user = Account::getUser($item['account_id']);

            // user角色为工人时,需要判断当前人是否是该工地的负责人
            if (!empty($user) && $user['role'] == Account::COMMON_ON) {
                $managerIds = AccountWorksite::where('worksite_id', $user['worksite_id'])->column('account_id');
                if (!in_array($accountId, $managerIds)) {
                    return $this->json(4003, '无此权限2');
                }
            }

            // 审核不通过
            if ($type == 0) {
                Account::where('id', $item['account_id'])->save(['checking' => Account::COMMON_OFF]);
                $item->save(['status' => -1, 'refund_reason' => $reason]);
                return $this->json();
            }

            $now = date('Y-m-d H:i:s');

            $update = $item->toArray();
            if ($update['is_register'] == CheckLog::COMMON_ON) {
                $update['role']    = CheckLog::COMMON_ON;
                $update['work_at'] = $now;
            }
            $update['checking'] = Account::COMMON_OFF;
            unset($update['id']);
            unset($update['account_id']);
            unset($update['is_register']);
            unset($update['status']);
            unset($update['created_at']);
            unset($update['check_at']);
            unset($update['refund_reason']);

            Account::where('id', $item['account_id'])->save($update);
            $item->save(['status' => 1, 'check_at' => $now]);
            return $this->json();
        } catch (Exception $e) {
            return $this->json(4000, '没有相关的用户记录'.$e->getMessage());
        }
    }

    // 打卡记录
    public function clockList(): Json
    {
        $page   = input('page/d', 1);
        $size   = input('size/d', 20);
        $status = input('status/d', 0);//状态 0=待审核 1=已审核(包含1通过 -1不通过)

        $accountId = $this->request->user['user_id'] ?? 0;

        if (!$account = Account::findById($accountId, ['id, role'])) {
            return $this->json(6001, '请先登录');
        }

        if ($account['role'] <= Account::COMMON_ON) {
            return $this->json(4003, '无权限');
        }

        $where = [];

        if ($status == 0) {
            $where[] = ['cl.status', '=', 0];
        } else {
            $where[] = ['cl.status', 'in', [1, -1]];
        }

        // 负责工地
        $worksiteIds = AccountWorksite::where('account_id', $accountId)->column('worksite_id');
        $where[]     = ['cl.worksite_id', 'in', $worksiteIds];

        $query = \app\model\ClockLog::alias('cl')
            ->leftJoin('account a', 'a.id = cl.account_id')
            ->leftJoin('worksite w', 'w.id = cl.worksite_id')
            ->field('cl.*,w.name as worksite_name,a.real_name as worker_name')
            ->where($where);

        $total = $query->count();

        $res = [
            'total'   => $total,
            'current' => $page ?: 1,
            'size'    => $size ?: 20,
            'list'    => new Collection(),
        ];

        if ($total > 0) {
            $res['list'] = $query->page($page, $size)->order('cl.id', 'desc')->select();
            $res['list']->each(function ($item) {
                $item->type_text = $item->type == 'in' ? '上班' : '下班';
                switch ($item->status) {
                    case 0:
                        $item->status_text = '待确认';
                        break;
                    case 1:
                        $item->status_text = '已确认';
                        break;
                    case -1:
                        $item->status_text = '不通过';
                        break;
                }
                unset($item->check_by);
                unset($item->check_at);
                unset($item->account_id);
                unset($item->create_time);
                unset($item->type);
                unset($item->worksite_id);
            });
            $res['list'] = arrayNullToString($res['list']->toArray());
        }

        return $this->json(0, 'success', $res);
    }

    /**
     * 审核打卡 支持批量
     *
     * @return Json
     */
    public function checkClock(): Json
    {
        try {
            $accountId = $this->request->user['user_id'] ?? 0;
            $type      = input('type/d', 1);//类型 1=通过 0=不通过
            $ids       = input('id/s');//待审核记录ID 多个用逗号分割
            $ids       = explode(',', $ids);
            $ids       = array_filter($ids);

            if (!in_array($type, [0, 1])) {
                return $this->json(4001, '审核参数错误');
            }

            if (!$account = Account::findById($accountId, ['id, role'])) {
                return $this->json(6001, '请先登录');
            }

            if ($account['role'] <= Account::COMMON_ON) {
                // 工地负责人才能操作
                return $this->json(4003, '无此权限');
            }

            $worksiteIds = AccountWorksite::where('account_id', $accountId)->column('worksite_id');
            if (ClockLog::whereIn('id', $ids)->whereNotIn('worksite_id', $worksiteIds)->count() > 0) {
                return $this->json(4003, '部分记录不在您权限操作的范围');
            }

            ClockLog::whereIn('id', $ids)->where('status', ClockLog::COMMON_OFF)->update([
                'status'   => $type == 1 ? 1 : -1,
                'check_at' => date('Y-m-d H:i:s'),
                'check_by' => $accountId
            ]);
            return $this->json();
        } catch (Exception $e) {
            return $this->json(5000, '审核失败'.$e->getMessage());
        }
    }

    // 加班记录
    public function overtimeList(): Json
    {
        $page   = input('page/d', 1);
        $size   = input('size/d', 20);
        $status = input('status/d', 0);//状态 0=待审核 1=已审核(包含1通过 -1不通过)

        $accountId = $this->request->user['user_id'] ?? 0;

        if (!$account = Account::findById($accountId, ['id, role'])) {
            return $this->json(6001, '请先登录');
        }

        if ($account['role'] <= Account::COMMON_ON) {
            return $this->json(4003, '无权限');
        }

        $where = [];

        if ($status == 0) {
            $where[] = ['cl.status', '=', 0];
        } else {
            $where[] = ['cl.status', 'in', [1, -1]];
        }

        // 负责工地
        $worksiteIds = AccountWorksite::where('account_id', $accountId)->column('worksite_id');
        $where[]     = ['cl.worksite_id', 'in', $worksiteIds];

        $query = \app\model\OvertimeLog::alias('cl')
            ->leftJoin('account a', 'a.id = cl.account_id')
            ->leftJoin('worksite w', 'w.id = cl.worksite_id')
            ->field('cl.*,w.name as worksite_name,a.real_name as worker_name')
            ->where($where);

        $total = $query->count();

        $res = [
            'total'   => $total,
            'current' => $page ?: 1,
            'size'    => $size ?: 20,
            'list'    => new Collection(),
        ];

        if ($total > 0) {
            $res['list'] = $query->page($page, $size)->order('cl.id', 'desc')->select();
            $res['list']->each(function ($item) {
                switch ($item->status) {
                    case 0:
                        $item->status_text = '待确认';
                        break;
                    case 1:
                        $item->status_text = '已确认';
                        break;
                    case -1:
                        $item->status_text = '不通过';
                        break;
                }
                $item->create_time = date('Y年m月d日 H:i:s', $item->create_time);
                $item->worktime    = date('Y年m月d日', strtotime($item->day));
                unset($item->check_by);
                unset($item->check_at);
                unset($item->account_id);
                unset($item->created_at);
                unset($item->type);
                unset($item->day);
                unset($item->day_text);
                unset($item->worksite_id);
                unset($item->is_statistic
                );
            });
            $res['list'] = arrayNullToString($res['list']->toArray());
        }

        return $this->json(0, 'success', $res);
    }

    /**
     * 审核加班 支持批量
     *
     * @return Json
     */
    public function checkOvertime(): Json
    {
        try {
            $accountId = $this->request->user['user_id'] ?? 0;
            $type      = input('type/d', 1);//类型 1=通过 0=不通过
            $ids       = input('id/s');//待审核记录ID 多个用逗号分割
            $ids       = explode(',', $ids);
            $ids       = array_filter($ids);

            if (!in_array($type, [0, 1])) {
                return $this->json(4001, '审核参数错误');
            }

            if (!$account = Account::findById($accountId, ['id, role'])) {
                return $this->json(6001, '请先登录');
            }

            if ($account['role'] <= Account::COMMON_ON) {
                // 工地负责人才能操作
                return $this->json(4003, '无此权限');
            }

            $worksiteIds = AccountWorksite::where('account_id', $accountId)->column('worksite_id');
            if (OvertimeLog::whereIn('id', $ids)->whereNotIn('worksite_id', $worksiteIds)->count() > 0) {
                return $this->json(4003, '部分记录不在您权限操作的范围');
            }

            OvertimeLog::whereIn('id', $ids)->where('status', ClockLog::COMMON_OFF)->update([
                'status'   => $type == 1 ? 1 : -1,
                'check_at' => date('Y-m-d H:i:s'),
                'check_by' => $accountId,
            ]);
            return $this->json();
        } catch (Exception $e) {
            return $this->json(5000, '审核失败'.$e->getMessage());
        }
    }

    // 工资记录MOCK
    public function payListMock(): Json
    {
        $page   = input('page/d', 1);
        $size   = input('size/d', 20);
        $status = input('status/d', -1);
        $date   = input('data/s', '');

        $accountId = $this->request->user['user_id'] ?? 0;

        $where = [];

        if (!empty($keyword)) {
            $where[] = ['w.name', 'like', '%'.$keyword.'%'];
        }

        if ($status >= 0) {
            $where[] = ['w.status', '=', $status];
        }

        if (!empty($date)) {
            $date    = str_replace('_', '', $date);
            $where[] = ['cl.time', '=', $date];
        }

        $where[] = ['cl.account_id', '=', $accountId];
        //        $query   = \app\model\ClockLog::alias('cl')
        //            ->leftJoin('worksite w', 'w.id = cl.worksite_id')
        //            ->field('cl.*,w.name as worksite_name')
        //            ->where($where);
        //
        //        $total = $query->count();
        $total = 20;

        $res = [
            'total'   => $total,
            'current' => $page ?: 1,
            'size'    => $size ?: 20,
            'info'    => [],
            'list'    => new Collection(),
        ];

        if ($total > 0) {
            $res['info'] = [
                'amount'          => 999999,//工资总金额
                'base_amount'     => 20000,//基本工资
                'overtime_amount' => 20000,//加班工资

                'not_amount'  => 20000,//未发放
                'done_amount' => 20000,//已发放
            ];
            $res['list'] = [
                [
                    'name'            => '张珊珊',
                    'status'          => 0,
                    'status_text'     => '待发放',
                    'base_amount'     => 7500,
                    'overtime_amount' => 0,
                    'amount'          => 7500,
                ],
                [
                    'name'            => '李珊珊',
                    'status'          => 0,
                    'status_text'     => '待发放',
                    'base_amount'     => 7500,
                    'overtime_amount' => 0,
                    'amount'          => 7500,
                ],
                [
                    'name'            => '王珊珊',
                    'status'          => 0,
                    'status_text'     => '待发放',
                    'base_amount'     => 7500,
                    'overtime_amount' => 0,
                    'amount'          => 7500,
                ],
                [
                    'name'            => '张珊珊',
                    'status'          => 0,
                    'status_text'     => '待发放',
                    'base_amount'     => 7500,
                    'overtime_amount' => 0,
                    'amount'          => 7500,
                ],
                [
                    'name'            => '刘珊珊',
                    'status'          => 0,
                    'status_text'     => '待发放',
                    'base_amount'     => 7500,
                    'overtime_amount' => 0,
                    'amount'          => 7500,
                ],
                [
                    'name'            => '谢珊珊',
                    'status'          => 0,
                    'status_text'     => '待发放',
                    'base_amount'     => 7500,
                    'overtime_amount' => 0,
                    'amount'          => 7500,
                ],
                [
                    'name'            => '吴珊珊',
                    'status'          => 0,
                    'status_text'     => '待发放',
                    'base_amount'     => 7500,
                    'overtime_amount' => 0,
                    'amount'          => 7500,
                ],
            ];
        }

        return $this->json(0, 'success', $res);
    }

    // 工资记录
    public function payList(): Json
    {
        $page   = input('page/d', 1);
        $size   = input('size/d', 20);
        $status = input('status/d', -1);
        $date   = input('date/s', '');

        $status = $status ?: -1;

        $accountId = $this->request->user['user_id'] ?? 0;

        $where = [];

        if (!empty($date)) {
            $date    = str_replace('_', '', $date);
            $where[] = ['pml.time', '=', $date];
        }

        $worksiteIds = AccountWorksite::where('account_id', $accountId)->column('worksite_id');

        $total = 0;
        $res   = [
            'total'   => $total,
            'current' => $page ?: 1,
            'size'    => $size ?: 20,
            'info'    => [],
            'list'    => new Collection(),
        ];

        if (empty($worksiteIds)) {
            return $this->json(0, 'success', $res);
        }

        // 当前明确说明 负责人和工地是一对一关系
        $where[] = ['pml.worksite_id', '=', $worksiteIds[0]];

        $query = \app\model\PayMonthLog::alias('pml')
            //            ->leftJoin('account a', 'a.id = pml.account_id')
            ->where($where);
        // 汇总信息
        $info = $query->fieldRaw('sum(pml.amount) as amount, sum(pml.base_amount) as base_amount,
            sum(pml.overtime_amount) as overtime_amount,sum(pml.paid_amount) as paid_amount')->find()->toArray();

        $res['info'] = [
            'amount'          => $info['amount'] ?? 0,
            'base_amount'     => $info['base_amount'] ?? 0,
            'overtime_amount' => $info['overtime_amount'] ?? 0,
            'done_amount'     => $info['paid_amount'] ?? 0,
            'not_amount'      => Math::sub($info['amount'] ?? 0, $info['paid_amount'] ?? 0),
        ];

        if ($status >= 0) {
            $where[] = ['pml.status', '=', $status];
        }

        $query = \app\model\PayMonthLog::alias('pml')
            ->leftJoin('account a', 'a.id = pml.account_id')
            ->where($where);

        $total        = $query->count();
        $res['total'] = $total;

        if ($total > 0) {
            $res['list'] = $query->field('pml.id,pml.status,pml.amount,pml.base_amount,pml.overtime_amount,pml.paid_amount,a.real_name as name')->page($page, $size)->order('pml.id', 'desc')->select();
            $res['list']->each(function ($item) {
                $item->status      = 0;
                $item->status_text = '待发放';

                if ($item['amount'] == $item['paid_amount'] && $item['amount'] > 0) {
                    $item->status_text = '完全发放';
                    $item->status      = 1;
                }

                if ($item['amount'] > $item['paid_amount'] && $item['paid_amount'] > 0) {
                    $item->status_text = '部分发放';
                    $item->status      = 2;
                }
            });
        }

        return $this->json(0, 'success', $res);
    }

    // 工人列表
    public function workerList(): Json
    {
        $page    = input('page/d', 1);
        $size    = input('size/d', 20);
        $keyword = input('keyword/s');

        $accountId = $this->request->user['user_id'] ?? 0;

        if (!$account = Account::findById($accountId, ['id, role'])) {
            return $this->json(6001, '请先登录');
        }

        if ($account['role'] <= Account::COMMON_ON) {
            return $this->json(4003, '无查看权限');
        }

        $where = [];

        if (!empty($keyword)) {
            $where[] = ['a.real_name|a.mobile', 'like', '%'.$keyword.'%'];
        }

        // 负责工地
        $worksiteIds = AccountWorksite::where('account_id', $accountId)->column('worksite_id');
        $where[]     = ['a.worksite_id', 'in', $worksiteIds];
        $where[]     = ['a.role', '=', Account::ROLE_WORKER];

        $query = Account::alias('a')
            ->leftJoin('worksite w', 'a.worksite_id = w.id')
            ->field('a.id,a.real_name,a.work_at,w.name as worksite_name')
            ->where($where);

        $total = $query->count();

        $res = [
            'total'   => $total,
            'current' => $page ?: 1,
            'size'    => $size ?: 20,
            'list'    => new Collection(),
        ];

        if ($total > 0) {
            $list        = $query->page($page, $size)->order('a.id', 'desc')->select();
            $accountList = $list->column('id');
            $starList    = Db::query("SELECT `star`,`account_id` FROM `bee_account_star` WHERE `id` IN (SELECT max(id) FROM `bee_account_star` WHERE FIND_IN_SET(account_id, ?) GROUP BY `account_id`)", [implode(',', $accountList)]);

            $starArr = [];
            foreach ($starList as $l) {
                $starArr[$l['account_id']] = $l['star'];
            }

            $res['list'] = $list;
            $res['list']->each(function ($item) use ($starArr) {
                $item->star = $starArr[$item->id] ?? 0;
            });
            $res['list'] = arrayNullToString($res['list']->toArray());
        }

        return $this->json(0, 'success', $res);
    }

    /**
     * 工人评定
     *
     * @return Json
     */
    public function star(): Json
    {
        try {
            $accountId = $this->request->user['user_id'] ?? 0;
            $star      = input('star/d', 5);//星级 默认5
            $id        = input('id/s');

            if (!in_array($star, [1, 2, 3, 4, 5])) {
                return $this->json(4001, '参数错误');
            }

            if (!$account = Account::findById($accountId, ['id, role, worksite_id'])) {
                return $this->json(6001, '请先登录');
            }

            if ($account['role'] <= Account::COMMON_ON) {
                // 工地负责人才能操作
                return $this->json(4003, '无此权限');
            }

            $ym = date('Ym');
            if (AccountStar::where('ym', $ym)->where('type', AccountStar::TYPE_WORKER)->where('account_id', $id)->count() > 0) {
                return $this->json(4001, '本月已评');
            }

            AccountStar::create([
                'type'        => AccountStar::TYPE_WORKER,
                'star'        => $star,
                'account_id'  => $id,
                'worksite_id' => $account['worksite_id'],
                'year'        => date('Y'),
                'month'       => date('m'),
                'ym'          => $ym,
                'operated_by' => $accountId,
                'created_at'  => date('Y-m-d H:i:s'),
            ]);
            return $this->json();
        } catch (Exception $e) {
            return $this->json(5000, '评定失败'.$e->getMessage());
        }
    }

    /**
     * 审核离职 支持批量
     *
     * @return Json
     */
    public function checkDimission(): Json
    {
        try {
            $accountId = $this->request->user['user_id'] ?? 0;
            $reason    = input('reason/s', '');
            $type      = input('type/d', 1);//类型 1=通过 0=不通过
            $ids       = input('id/s');//待审核记录ID 多个用逗号分割
            $ids       = explode(',', $ids);
            $ids       = array_filter($ids);

            if (!in_array($type, [0, 1])) {
                return $this->json(4001, '审核参数错误');
            }

            if (!$account = Account::findById($accountId, ['id, role'])) {
                return $this->json(6001, '请先登录');
            }

            if ($account['role'] <= Account::COMMON_ON) {
                // 工地负责人才能操作
                return $this->json(4003, '无此权限');
            }

            $worksiteIds = AccountWorksite::where('account_id', $accountId)->column('worksite_id');
            if (AccountDimission::whereIn('id', $ids)->whereNotIn('worksite_id', $worksiteIds)->count() > 0) {
                return $this->json(4003, '部分记录不在您权限操作的范围');
            }

            AccountDimission::whereIn('id', $ids)->where('status', AccountDimission::COMMON_OFF)->update([
                'status'        => $type == 1 ? 1 : -1,
                'operated_at'   => date('Y-m-d H:i:s'),
                'operated_by'   => $accountId,
                'refuse_reason' => $reason,
            ]);

            // 账号更新
            $accountIds = AccountDimission::whereIn('id', $ids)->column('account_id');
            Account::whereIn('id', $accountIds)->where('role', Account::ROLE_WORKER)->update([
                'role'        => Account::ROLE_NORMAL,
                'worksite_id' => Account::COMMON_OFF,
                'checking'    => Account::COMMON_OFF,
            ]);
            return $this->json();
        } catch (Exception $e) {
            return $this->json(5000, '审核失败'.$e->getMessage());
        }
    }

    // 离职审核列表
    public function dimissionCheckList(): Json
    {
        $page    = input('page/d', 1);
        $size    = input('size/d', 20);
        $keyword = input('keyword/s');
        $status  = input('status/d', 0);//状态 0=待审核 1=已审核(包含1通过 -1不通过)

        $accountId = $this->request->user['user_id'] ?? 0;

        if (!$account = Account::findById($accountId, ['id, role'])) {
            return $this->json(6001, '请先登录');
        }

        if ($account['role'] <= Account::COMMON_ON) {
            return $this->json(4003, '无查看权限');
        }

        $where = [];

        if (!empty($keyword)) {
            $where[] = ['cl.real_name|cl.mobile', 'like', '%'.$keyword.'%'];
        }

        if ($status == 0) {
            $where[] = ['cl.status', '=', 0];
        } else {
            $where[] = ['cl.status', 'in', [1, -1]];
        }

        // 负责工地
        $worksiteIds = AccountWorksite::where('account_id', $accountId)->column('worksite_id');
        $where[]     = ['cl.worksite_id', 'in', $worksiteIds];

        $query = AccountDimission::alias('cl')
            ->leftJoin('account a', 'a.id = cl.account_id')
            ->leftJoin('worksite w', 'w.id = cl.worksite_id')
            ->leftJoin('position p', 'p.id = a.position')
            ->field('cl.id,a.real_name,cl.status,cl.created_at,p.name as position_name,a.work_at,w.name as worksite_name')
            ->where($where);

        $total = $query->count();

        $res = [
            'total'   => $total,
            'current' => $page ?: 1,
            'size'    => $size ?: 20,
            'list'    => new Collection(),
        ];

        if ($total > 0) {
            $res['list'] = $query->page($page, $size)->order('cl.id', 'desc')->select();
            //            $res['list']->each(function ($item) {
            //                $item->created_at = date('Y年m月d日 H:i:s', strtotime($item->created_at));
            //            });
            $res['list'] = arrayNullToString($res['list']->toArray());
        }

        return $this->json(0, 'success', $res);
    }

    // 保存|编辑公告
    public function saveNotice(): Json
    {
        $status  = input('status/d', 0);
        $content = input('content/s');

        $accountId = $this->request->user['user_id'] ?? 0;

        if (!$account = Account::findById($accountId, ['id, role, worksite_id'])) {
            return $this->json(6001, '请先登录');
        }

        if ($account['role'] <= Account::COMMON_ON) {
            // 工地负责人才能操作
            return $this->json(4003, '无此权限');
        }

        if (!$worksite = Worksite::findById($account['worksite_id'])) {
            return $this->json(4004, '工地异常');
        }

        $update = [
            'status'  => $status,
            'content' => $content,
        ];

        $oldNotice = json_decode($worksite['notice'], true);
        if ($oldNotice) {
            $update['version'] = $oldNotice['content'] == $content ? $oldNotice['version'] : time();
        } else {
            $update['version'] = time();
        }


        $worksite->save([
            'notice' => json_encode($update, JSON_UNESCAPED_UNICODE)
        ]);
        return $this->json();
    }

    // 补卡记录
    public function replenishList(): Json
    {
        $page   = input('page/d', 1);
        $size   = input('size/d', 20);
        $status = input('status/d', 0);//状态 0=待审核 1=已审核(包含1通过 -1不通过)

        $accountId = $this->request->user['user_id'] ?? 0;

        if (!$account = Account::findById($accountId, ['id, role'])) {
            return $this->json(6001, '请先登录');
        }

        if ($account['role'] <= Account::COMMON_ON) {
            return $this->json(4003, '无权限');
        }

        $where = [];

        if ($status == 0) {
            $where[] = ['cl.status', '=', 0];
        } else {
            $where[] = ['cl.status', 'in', [1, -1]];
        }

        // 负责工地
        $worksiteIds = AccountWorksite::where('account_id', $accountId)->column('worksite_id');
        $where[]     = ['cl.worksite_id', 'in', $worksiteIds];
        $where[]     = ['cl.is_replenish', '=', ClockLog::COMMON_ON];

        $query = \app\model\ClockLog::alias('cl')
            ->leftJoin('account a', 'a.id = cl.account_id')
            ->leftJoin('worksite w', 'w.id = cl.worksite_id')
            ->field('cl.*,w.name as worksite_name,a.real_name as worker_name')
            ->where($where);

        $total = $query->count();

        $res = [
            'total'   => $total,
            'current' => $page ?: 1,
            'size'    => $size ?: 20,
            'list'    => new Collection(),
        ];

        if ($total > 0) {
            $res['list'] = $query->page($page, $size)->order('cl.id', 'desc')->select();
            $res['list']->each(function ($item) {
                switch ($item->status) {
                    case 0:
                        $item->status_text = '待确认';
                        break;
                    case 1:
                        $item->status_text = '已确认';
                        break;
                    case -1:
                        $item->status_text = '不通过';
                        break;
                }
                $item->create_time = date('Y年m月d日 H:i:s', $item->create_time);
                $typeText          = ClockLog::typeText()[$item->type] ?? '';
                $dayText           = date('Y年m月d日', strtotime($item->day));
                $item->day_text    = $dayText;
                $item->type_text   = $typeText;
                $item->desc        = "补打$dayText $typeText 打卡";
                unset($item->check_by);
                unset($item->check_at);
                unset($item->account_id);
                unset($item->created_at);
                unset($item->worksite_id);
                unset($item->is_statistic);
                unset($item->need_statistic);
                unset($item->indexs);
                unset($item->handle_count);
                unset($item->is_replenish);
                unset($item->role);
                unset($item->type);
            });
            $res['list'] = arrayNullToString($res['list']->toArray());
        }

        return $this->json(0, 'success', $res);
    }

    /**
     * 审核补卡 支持批量
     *
     * @return Json
     */
    public function checkReplenish(): Json
    {
        try {
            $accountId = $this->request->user['user_id'] ?? 0;
            $type      = input('type/d', 1);//类型 1=通过 0=不通过
            $ids       = input('id/s');//待审核记录ID 多个用逗号分割
            $ids       = explode(',', $ids);
            $ids       = array_filter($ids);

            if (!in_array($type, [0, 1])) {
                return $this->json(4001, '审核参数错误');
            }

            if (!$account = Account::findById($accountId, ['id, role'])) {
                return $this->json(6001, '请先登录');
            }

            if ($account['role'] <= Account::COMMON_ON) {
                // 工地负责人才能操作
                return $this->json(4003, '无此权限');
            }

            $worksiteIds = AccountWorksite::where('account_id', $accountId)->column('worksite_id');
            if (ClockLog::whereIn('id', $ids)->whereNotIn('worksite_id', $worksiteIds)->count() > 0) {
                return $this->json(4003, '部分记录不在您权限操作的范围');
            }

            ClockLog::whereIn('id', $ids)->where('status', ClockLog::COMMON_OFF)->update([
                'status'   => $type == 1 ? 1 : -1,
                'check_at' => date('Y-m-d H:i:s'),
                'check_by' => $accountId,
            ]);
            return $this->json();
        } catch (Exception $e) {
            return $this->json(5000, '审核失败'.$e->getMessage());
        }
    }
}