<?php
/**
 *
 * @author：llbjj
 * @DateTime：2022/5/6 10:57
 * @Description：
 *
 */

namespace Application\Service\DB\Business;

use Interop\Container\ContainerInterface;
use Laminas\Db\Sql\Expression;

class Record extends \Application\Service\DB\AbstractDb
{
    public function __construct($dbAdapter, ContainerInterface $container)
    {
        $this->tableName = 'yikeen_business_record';
        parent::__construct($dbAdapter, $container);
    }

    /**
     * Notes: 更新业务流程联系人信息
     * User: lltyy
     * DateTime: 2022/11/21 14:49
     *
     * @param $arr
     * @return int
     */
    public function updateWorkUser($arr){
        $parent_item = $arr['parent_item'] ? $arr['parent_item'] : 0;//主项目ID
        $item_id = $arr['item_id'] ? $arr['item_id'] : 0;//副项目ID
        $itemsig_id = $arr['itemsig_id'] ? $arr['itemsig_id'] : 0;//项目中心ID
        $user_id = $arr['user_id'] ? $arr['user_id'] : 0;//工作流程人员ID
        $where = [
            'is_del' => 0,
            new Expression('find_in_set(?, worklistset_ids)', $user_id),
        ];
        if(!empty($parent_item)){
            $where['item_id'] = $parent_item;
        }
        if(!empty($item_id)){
            $item_fid = $this->LocalService()->itemInfo->getOneFieldVal('fid',[
                'id' => $item_id,
            ]);
            if(!empty($item_fid)){
                $where['item_id'] = $item_fid;
            }else{
                $where = [
                    'id' => 0,
                ];
            }
        }
        if(!empty($itemsig_id)){
            $where['itemsig_id'] = $itemsig_id;
        }
        $cond = [
            'where'=>$where,
            'columns'=>['id','worklistset_ids']
        ];
        $datas = $this->fetchAll($cond);
        if(!empty($datas)){
            foreach($datas as $data){
                $data_user_arr = explode(',',$data['worklistset_ids']);
                $delete_key = array_search($user_id,$data_user_arr);
                unset($data_user_arr[$delete_key]);
                $data_users = !empty($data_user_arr) ? implode(',',$data_user_arr) : "";
                $this->update(['worklistset_ids'=>$data_users],['id'=>$data['id']]);
            }
        }
        return 200;
    }

}