package com.zbkj.admin.controller;

import com.zbkj.common.model.system.SystemHelpProblem;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.SystemHelpRequest;
import com.zbkj.common.response.CommonResult;
import com.zbkj.service.service.HelpService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

@Slf4j
@RestController
@RequestMapping("api/admin/system/helpProblem")
@Api(tags = "系统帮助问题")
public class SystemHelpController {

    @Autowired
    private HelpService helpService;

    @RequestMapping("/list")
    @PreAuthorize("hasAuthority('admin:helpProblem:list')")
    public CommonResult<CommonPage<SystemHelpProblem>> list(@Validated SystemHelpRequest request, @Validated PageParamRequest page) {
        SystemHelpProblem entity = new SystemHelpProblem();
        BeanUtils.copyProperties(request, entity);
        List<SystemHelpProblem> list = helpService.getListHelp(entity, page);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('admin:helpProblem:info')")
    public CommonResult<SystemHelpProblem> info(@PathVariable Integer id) {
        SystemHelpProblem entity = helpService.getById(id);
        return CommonResult.success(entity);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('admin:helpProblem:save')")
    public CommonResult<String> save(@RequestBody SystemHelpRequest request) {
        SystemHelpProblem entity = new SystemHelpProblem();
        BeanUtils.copyProperties(request, entity);
        if (helpService.save(entity)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('admin:helpProblem:update')")
    public CommonResult<String> update(@RequestBody SystemHelpRequest request) {
        SystemHelpProblem entity = new SystemHelpProblem();
        BeanUtils.copyProperties(request, entity);
        if (helpService.save(entity)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('admin:helpProblem:delete')")
    public CommonResult<String> delete(@RequestBody Integer[] ids) {
        System.out.println("111");
//        if (helpService.removeByIds(Arrays.asList(ids))) {
            return CommonResult.success();
//        }
//        return CommonResult.failed();
    }
}
