<?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\controller\works;


use app\adminapi\controller\BaseAdminController;
use app\adminapi\lists\works\ReturnWorkLists;
use app\adminapi\logic\works\IssueWorkLogic;
use app\adminapi\logic\works\ReturnWorkLogic;
use app\adminapi\logic\works\ServiceWorkLogic;
use app\adminapi\validate\works\ReturnWorkValidate;


/**
 * ReturnWork控制器
 * Class ReturnWorkController
 * @package app\adminapi\controller\works
 */
class ReturnWorkController extends BaseAdminController
{


    /**
     * @notes 获取列表
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/07/10 15:06
     */
    public function lists()
    {
        return $this->dataLists(new ReturnWorkLists());
    }


    /**
     * @notes 添加
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/07/10 15:06
     */
    public function add()
    {
        $params = (new ReturnWorkValidate())->post()->goCheck('add');
        $result = ReturnWorkLogic::add($params);
        if (true === $result) {
            return $this->success('添加成功', [], 1, 1);
        }
        return $this->fail(ReturnWorkLogic::getError());
    }


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


    /**
     * @notes 删除
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/07/10 15:06
     */
    public function delete()
    {
        $params = (new ReturnWorkValidate())->post()->goCheck('delete');
        ReturnWorkLogic::delete($params);
        return $this->success('删除成功', [], 1, 1);
    }


    /**
     * @notes 获取详情
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/07/10 15:06
     */
    public function detail()
    {
        $params = (new ReturnWorkValidate())->goCheck('detail');
        $result = ReturnWorkLogic::detail($params);
        return $this->data($result);
    }

    /**
     * 处理返修工单
     * @return \think\response\Json
     * @author 林海涛
     * @date 2024/7/24 下午1:52
     */
    public function issueWorkData()
    {
        $params = (new ReturnWorkValidate())->post()->goCheck('issueWorkData');
        $result = ReturnWorkLogic::issueWorkData($params);
        if (true === $result) {
            $issueWork = IssueWorkLogic::detail(['id'=>$params['issue_work_id']]);
            // 订单返修通知【给用户】
            event('Notice',  [
                'scene_id' => 121,
                'params' => [
                    'user_id' => $issueWork['user_id']
                ]
            ]);
            // 返修工程师预约上门通知【给工程师的通知】
            $returnWork = ReturnWorkLogic::detail(['issue_work_id'=>$params['issue_work_id']]);
            $workDetail = ServiceWorkLogic::detail(['id'=>$issueWork['service_work_id']]);
            event('Notice',  [
                'scene_id' => 124,
                'params' => [
                    'user_id' => $returnWork['master_worker_id'],
                    'order_id' => $params['issue_work_id'],
                    'thing2' => $issueWork['complaint_type_text'],
                    'thing3' => $issueWork['complaint_details'],
                    'thing4' => (iconv_strlen($workDetail['address'])>15)?(mb_substr($workDetail['address'],0,15,'UTF-8').'...'):$workDetail['address'],
                    'phone_number6' => asteriskString($issueWork['complaint_mobile']),
                    'time17' => $returnWork['appointment_time'],
                ]
            ]);
            return $this->success('成功', [], 1, 1);
        }
        return $this->fail(ReturnWorkLogic::getError());
    }
}