package com.ruoyi.web.modules.farming.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
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.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.modules.farming.domain.BizTechResource;
import com.ruoyi.web.modules.farming.service.IBizTechResourceService;
import com.ruoyi.web.modules.farming.support.TechResourceTypeValidator;
import com.ruoyi.web.modules.farming.support.TechResourceValidation;

/**
 * 畜牧科技资源管理
 */
@RestController
@RequestMapping("/techService/techResource")
public class BizTechResourceController extends BaseController
{
    @Autowired
    private IBizTechResourceService bizTechResourceService;

    @Autowired
    private TechResourceTypeValidator techResourceTypeValidator;

    @PreAuthorize("@ss.hasPermi('techService:techResource:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizTechResource query,
            @RequestParam(value = "publishStatus", required = false) Integer listPublishStatus)
    {
        if (listPublishStatus != null)
        {
            query.setQueryPublishStatus(listPublishStatus);
            query.setPublishStatus(null);
        }
        // 须在 startPage 之前完成校验：assertAllowed 会查库，否则 PageHelper 分页会落在类别查询上
        TechResourceValidation.validateListQuery(query);
        techResourceTypeValidator.assertAllowed(query.getResourceType());
        startPage();
        List<BizTechResource> list = bizTechResourceService.selectBizTechResourceList(query);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('techService:techResource:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizTechResourceService.selectBizTechResourceById(id));
    }

    @PreAuthorize("@ss.hasPermi('techService:techResource:add')")
    @Log(title = "畜牧科技资源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizTechResource row)
    {
        row.setCreateBy(getUsername());
        return toAjax(bizTechResourceService.insertBizTechResource(row));
    }

    @PreAuthorize("@ss.hasPermi('techService:techResource:edit')")
    @Log(title = "畜牧科技资源", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizTechResource row)
    {
        row.setUpdateBy(getUsername());
        return toAjax(bizTechResourceService.updateBizTechResource(row));
    }

    @PreAuthorize("@ss.hasPermi('techService:techResource:remove')")
    @Log(title = "畜牧科技资源", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bizTechResourceService.deleteBizTechResourceByIds(ids, getUsername()));
    }

    @PreAuthorize("@ss.hasPermi('techService:techResource:publish')")
    @Log(title = "畜牧科技资源发布", businessType = BusinessType.UPDATE)
    @PostMapping("/publish/{id}")
    public AjaxResult publish(@PathVariable Long id)
    {
        return toAjax(bizTechResourceService.publish(id));
    }

    @PreAuthorize("@ss.hasPermi('techService:techResource:offline')")
    @Log(title = "畜牧科技资源下架", businessType = BusinessType.UPDATE)
    @PostMapping("/offline/{id}")
    public AjaxResult offline(@PathVariable Long id)
    {
        return toAjax(bizTechResourceService.offline(id));
    }

    @PreAuthorize("@ss.hasPermi('techService:techResource:assignAccount')")
    @Log(title = "畜牧科技资源分配账号", businessType = BusinessType.INSERT)
    @PostMapping("/assignAccount/{id}")
    public AjaxResult assignAccount(@PathVariable Long id)
    {
        return toAjax(bizTechResourceService.assignAccount(id, getUsername()));
    }
}
