package com.cool.bang.modules.business.controller;

import com.cool.bang.common.base.BaseController;
import com.cool.bang.common.constant.ControllerConstant;
import com.cool.bang.common.web.request.PageDomain;
import com.cool.bang.common.web.response.Result;
import com.cool.bang.common.web.response.module.ResultTable;
import com.cool.bang.modules.business.domain.JobPolicy;
import com.cool.bang.modules.business.service.IJobPolicyService;
import com.github.pagehelper.PageInfo;
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.*;

/**
 * 政策
 *
 * @author Sy
 * @version 1.0.0
 * 2024-11-12 10:21
 */
@RestController
@RequestMapping(ControllerConstant.BUSINESS + "policy/v1")
public class JobPolicyController extends BaseController {

    private IJobPolicyService jobPolicyService;

    @Autowired
    public JobPolicyController(IJobPolicyService jobPolicyService) {
        this.jobPolicyService = jobPolicyService;
    }

    @GetMapping
    @PreAuthorize("hasPermission('','business:policy:main')")
    public ResultTable main(PageDomain pageDomain, JobPolicy param) {
        PageInfo<JobPolicy> jobPolicyPage = jobPolicyService.page(pageDomain, param);
        return pageTable(jobPolicyPage.getList(), jobPolicyPage.getTotal());
    }

    @GetMapping("{id}")
    @PreAuthorize("hasPermission('','business:policy:main')")
    public Result get(@PathVariable Long id) {
        return success(jobPolicyService.queryById(id));
    }

    @PostMapping
    @PreAuthorize("hasPermission('','business:policy:add')")
    public Result add(@Validated JobPolicy jobPolicy) {
        jobPolicy.insertConfigure();
        return decide(jobPolicyService.save(jobPolicy));
    }

    @PostMapping("edit")
    @PreAuthorize("hasPermission('','business:policy:edit')")
    public Result edit(@Validated JobPolicy jobPolicy) {
        return decide(jobPolicyService.update(jobPolicy));
    }

    @DeleteMapping("{id}")
    @PreAuthorize("hasPermission('','business:policy:del')")
    public Result remove(@PathVariable Integer id) {
        return decide(jobPolicyService.removeById(id));
    }

}
