<?php

namespace app\api\controller;

use app\adminapi\logic\fence_house\FenceHouseLogic;
use app\adminapi\validate\fence_house\FenceHouseValidate;
use app\api\lists\FenceHouseLists;
use app\common\model\fence_house\FenceHouse;
use think\facade\Db;
use think\response\Json;

class FenceHouseController extends BaseApiController
{
    public function list()
    {
        return $this->dataLists(new FenceHouseLists());
    }
    public function add()
    {
        $params = (new FenceHouseValidate())->post()->goCheck('add');
        $result = FenceHouseLogic::add($params);
        if (true === $result) {
            return $this->success('添加成功', [], 1, 1);
        }
        return $this->fail(FenceHouseLogic::getError());
    }


    /**
     * @notes 编辑
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/01/10 15:15
     */
    public function edit()
    {
        $params = (new FenceHouseValidate())->post()->goCheck('edit');
        $result = FenceHouseLogic::edit($params);
        if (true === $result) {
            return $this->success('编辑成功', [], 1, 1);
        }
        return $this->fail(FenceHouseLogic::getError());
    }

    public function detail()
    {
        $params = (new FenceHouseValidate())->goCheck('detail');
        $result = FenceHouseLogic::detail($params);
        return $this->data($result);
    }

    /**
     * @return Json
     */
    public function datas(): Json
    {
        $params['user_id'] =  $this->userId;
        $datas = (new FenceHouseLogic())->datas($params);
        return $this->data($datas);
    }

    // 离栏操作
    public function leave()
    {
        $params = $this->request->param();
        $params['user_id'] = $this->userId;
        $params['create_time'] = date('Y-m-d H:i:s', time());
        $params['update_time'] = date('Y-m-d H:i:s', time());
        Db::name('leave_fence_house_log')->insert($params);
        return $this->success('离栏成功');
    }

    // 离栏列表
    public function leaveList()
    {
        $params = $this->request->param();
        $pageNo =  $params['page_no'] ?? 1;
        $where = [];
        if (!empty($params['keyword'])) {
            $where[] = ['animal_sn', 'like', '%' . $params['keyword'] . '%'];
        }
        $lists = Db::name('leave_fence_house_log')->where('user_id', $this->userId)->where($where)->append(['fence_house_name'])->withAttr('fence_house_name',function ($value, $data){
            return FenceHouse::where('id', $data['fence_house_id'])->value('fence_house_name');
        })->page($pageNo, $params['page_size'])->select();
        $data = [
            'lists' => $lists,
            'count' => Db::name('leave_fence_house_log')->where('user_id', $this->userId)->count(),
            'page_no' => $pageNo,
            'page_size' => $params['page_size'],
        ];
        return $this->success('成功', $data);
    }

    // 换栏操作
    public function exchange()
    {
        $params = $this->request->param();
        $params['user_id'] = $this->userId;
        $params['create_time'] = date('Y-m-d H:i:s', time());
        $params['update_time'] = date('Y-m-d H:i:s', time());
        Db::name('change_fence_house_log')->insert($params);
        return $this->success('换栏成功');
    }

    public function exchangeList()
    {
        $params = $this->request->param();
        $where = [];
        if (!empty($params['keyword'])) {
            $where[] = ['animal_sn', 'like', '%' . $params['keyword'] . '%'];
        }
        $pageNo =  $params['page_no'] ?? 1;
        $lists = Db::name('change_fence_house_log')->where('user_id', $this->userId)->where($where)->append(['old_fence_house_name', 'new_fence_house_name'])
            ->withAttr('old_fence_house_name',function ($value, $data){
                return FenceHouse::where('id', $data['old_fence_house_id'])->value('fence_house_name');
            })
            ->withAttr('new_fence_house_name',function ($value, $data){
                return FenceHouse::where('id', $data['new_fence_house_id'])->value('fence_house_name');
            })
            ->page($pageNo, $params['page_size'])
            ->select();
        $data = [
            'lists' => $lists,
            'count' => Db::name('change_fence_house_log')->where('user_id', $this->userId)->count(),
            'page_no' => $pageNo,
            'page_size' => $params['page_size'],
        ];
        return $this->success('成功', $data);
    }

}