| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 | <?phpnamespace app\admin\controller\marketing;use app\admin\service\sys_manage\DeptService;use app\admin\validate\sys_manage\DeptValidate;use app\controller\Curd;use app\model\SysDept;use support\Db;use support\exception\BusinessException;use support\Request;use support\Response;class DeptPremisesController extends Curd{    public function __construct()    {        $this->model = new SysDept();        $this->validate = true;        $this->validateClass = new DeptValidate();    }    public function selectList()    {        $premisses = SysDept::where('dept_category', '营业场所')            ->select('dept_id as key', 'dept_name as label')            ->get()            ->toArray();        return json_success('', $premisses);    }    /**     * @Desc 营业场所列表     * @Author Gorden     * @Date 2024/3/7 13:17     *     * @param Request $request     * @return Response     * @throws BusinessException     */    public function select(Request $request): Response    {        [$where, $format, $limit, $field, $order] = $this->selectInput($request);        $order = $request->get('order', 'desc');        $field = $request->get('field', 'dept_addtimes');        $where['dept_super_id'] = 0;        $where['dept_category'] = '营业场所';        $query = $this->doSelect($where, $field, $order);        return $this->doFormat($query, $format, $limit);    }    public function afterQuery($items)    {        foreach ($items as &$item) {            if (!empty($item->dept_extend_json)) {                $extendJson = json_decode($item->dept_extend_json, true);                $item->dept_week = $extendJson['week'] ?? [];                $item->dept_work = [];                $deptWork = [];                if (!empty($extendJson['times'])) {                    $times = explode('~', $extendJson['times']);                    $deptWork[] = date('Y-m-d H:i:s', strtotime(date('Y-m-d ') . $times[0]));                    $deptWork[] = date('Y-m-d H:i:s', strtotime(date('Y-m-d ') . $times[1]));                }                $item->dept_work = $deptWork;            }        }        return $items;    }    public function afterInfoQuery($item)    {        if (!empty($item->dept_city)) {            $item->dept_city = explode(',', $item->dept_city);        }        return $item;    }    /**     * @Desc 添加营业场所     * @Author Gorden     * @Date 2024/3/7 11:48     *     * @param Request $request     * @return Response     */    public function insert(Request $request): Response    {        if ($this->validate && !$this->validateClass->scene('add_premises')->check($request->post())) {            return json_fail($this->validateClass->getError());        }        // 验证编号是否存在        if (!empty($request->post('dept_code')) && SysDept::checkExist('营业场所', 'dept_code', $request->post('dept_code'))) {            return json_fail('编号重复,请重新输入');        }        Db::beginTransaction();        try {            $data = $this->insertInput($request);            $data['dept_super_id'] = 0;            $deptId = $this->doInsert($data);            if (!$deptId) {                throw new BusinessException('创建营业场所失败');            }            // 更新部门path            $path = '/0/' . $deptId . '/';            if (!SysDept::where('dept_id', $deptId)->update(['dept_super_path' => $path])) {                throw new BusinessException('创建营业场所失败');            }            Db::commit();        } catch (BusinessException $customException) {            Db::rollBack();            return json_fail($customException->getMessage());        } catch (\Exception $exception) {            dump($exception->getMessage());            Db::rollBack();            return json_fail('创建营业场所失败');        }        return json_success('success');    }    protected function insertInput(Request $request): array    {        $data = $this->inputFilter($request->post());        return $data;    }    /**     * @Desc 更新     * @Author Gorden     * @Date 2024/3/7 13:14     *     * @param Request $request     * @return Response     */    public function update(Request $request): Response    {        if ($this->validate && !$this->validateClass->scene('update_premises')->check($request->post())) {            return json_fail($this->validateClass->getError());        }        try {            [$id, $data] = $this->updateInput($request);            $this->doUpdate($id, $data);        } catch (BusinessException $customException) {            return json_fail($customException->getMessage());        } catch (\Exception $e) {            return json_fail('数据更新失败');        }        return json_success('success');    }    /**     * 更新前置方法     * @param Request $request     * @return array     * @throws BusinessException     */    protected function updateInput(Request $request): array    {        $work = $request->post('dept_work', []);        $dateWeek = $request->post('dept_week', []);        $primary_key = $this->model->getKeyName();        $id = $request->post($primary_key);        $data = $this->inputFilter($request->post());        $model = $this->model->find($id);        if (!$model) {            throw new BusinessException('记录不存在', 2);        }        $extendJson = [];        if (!empty($model->dept_extend_json)) {            $extendJson = json_decode($model->dept_extend_json, true);        }        if (isset($work[0]) && isset($work[1])) {            $start = date('H:i', strtotime($work[0]));            $end = date('H:i', strtotime($work[1]));            $extendJson['times'] = $start . '~' . $end;        } elseif (!isset($work[0]) && isset($extendJson['times'])) {            unset($extendJson['times']);        }        if ($dateWeek) {            $newDates = [];            foreach ($dateWeek as $date) {                $key = self::$week[$date];                $newDates[$key] = $date;            }            ksort($newDates);            $currentDate = current($newDates);            $lastDate = end($newDates);            $weekStr = '';            if (self::$week[$lastDate] - self::$week[$currentDate] + 1 > count($newDates)) {                $weekStr = implode(',', $newDates);            } else if (self::$week[$lastDate] - self::$week[$currentDate] + 1 == count($newDates)) {                $weekStr = $currentDate . '至' . $lastDate;            }            $extendJson['weeks'] = $weekStr;            $extendJson['week'] = $dateWeek;        }        $data['dept_extend_json'] = json_encode($extendJson);        unset($data[$primary_key]);        return [$id, $data];    }    public static $week = [        '周一' => 1,        '周二' => 2,        '周三' => 3,        '周四' => 4,        '周五' => 5,        '周六' => 6,        '周日' => 7,    ];}
 |