package quant.rich.emoney.controller.manage;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import quant.rich.emoney.controller.common.UpdateBoolServiceController;
import quant.rich.emoney.entity.sqlite.RequestInfo;
import quant.rich.emoney.exception.RException;
import quant.rich.emoney.pojo.dto.LayPageReq;
import quant.rich.emoney.pojo.dto.LayPageResp;
import quant.rich.emoney.pojo.dto.R;
import quant.rich.emoney.service.sqlite.RequestInfoService;

@Slf4j
@Controller
@RequestMapping("/admin/v1/manage/requestInfo")
public class RequestInfoControllerV1 extends UpdateBoolServiceController<RequestInfo> {
    
    @Autowired
    RequestInfoService requestInfoService;
    
    @GetMapping({"", "/", "/index"})
    public String index() {
        return "/admin/v1/manage/requestInfo/index";
    }
	
	@GetMapping("/list")
	@ResponseBody
	public LayPageResp<?> list(LayPageReq<RequestInfo> pageReq) {
	    return super.list(pageReq);
	}
	
	@GetMapping("/getOne")
	@ResponseBody
	public R<?> getOne(Integer id) {
	    return super.getOne(id);
	}
	
	@PostMapping("/save")
	@ResponseBody
	public R<?> save(@RequestBody @NonNull RequestInfo requestInfo) {
	    return super.save(requestInfo);
	}
	
	@PostMapping("/delete")
	@ResponseBody
	public R<?> delete(String id) {
	    return super.delete(id);
	}
	
	@Override
    protected
	R<?> updateBool(String id, String field, Boolean value) {
	    return super.updateBool(id, field, value);
	}
	
	@PostMapping("/batchOp")
    @ResponseBody
    public R<?> batchOp(
            @RequestParam(value="ids[]", required=true)
            @Valid @NotEmpty String[] ids, @NotNull RequestInfoBatchOp op) {
        
	    List<String> idArray = Arrays.asList(ids);
	    
	    if (op == RequestInfoBatchOp.DELETE) {
	        return R.judge(getThisService().removeByIds(idArray));
	    }

        LambdaUpdateWrapper<RequestInfo> uw = new LambdaUpdateWrapper<>();
        uw.in(RequestInfo::getId, idArray);
        switch (op) {
        default:
            throw RException.badRequest("未知操作");
        }
    }
	
    private static enum RequestInfoBatchOp {
        DELETE,
        ENABLE_ANONYMOUS,
        DISABLE_ANONYMOUS
    }

}
