/**
* 啓用
*/
public function enable(){
return $this->setField('is_hid',0,$this->setFieldCallback('setInc'));
}
/**
* 禁用
*/
public function disable()
{
return $this->setField('is_hid',1,$this->setFieldCallback('setDec'));
}
/**
* 刪除
*/
public function del()
{
return $this->setField('is_del',1,$this->setFieldCallback('setDec'));
}
/**
* @param string $funcName
* @return \Closure
*/
private function setFieldCallback (string $funcName) {
return function (Int $Count,Int $Pid) use ($funcName){
return M($this->parentTB)->where('id='.$Pid)->$funcName($this->setField,$Count);
};
}
/**
* 更新字段
* @param $field 更新字段
* @param $value 更新值
* @param callable $callback 回調函數
* @author lxy
* @time 2020-04-14
*/
private function setField($field,$value,callable $callback ){
if (!is_callable($callback)) {
$this->error('Not a function ');
}
$ids = I('ids');
if (empty($ids)) {
$this->error('請選擇操作數據');
}
$temp = array(1,0);
$map = array(
'id' => ['IN',$ids],
'is_hid' => $temp[$value]
);
$okCount = (int) M($this->table)->where($map)->count();
if ($okCount < 1) {
$this->error('暫無可操作數據');
}
//問題id
$pid = (int) $_REQUEST['pid'];
$M = M();
$M -> startTrans();
try {
//更新答案狀態
$res_1 = M($this->table)->where($map)->setField($field,$value);
if (!is_numeric($res_1)) {
throw new \Exception('setField Error');
}
//更新問題回答數量
$res_2 = $callback($okCount,$pid);
if (!is_numeric($res_2)) {
throw new \Exception('callback Error');
}
$M -> commit();
$this->success('操作完成');
} catch (\Exception $e) {
$this->error($e->getMessage());
}
}