package com.ruoyi.web.modules.diagnosis.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.diagnosis.domain.BizMedicalResource;
import com.ruoyi.web.modules.diagnosis.domain.MedicalResourceBatchIdsBody;
import com.ruoyi.web.modules.diagnosis.service.IBizMedicalResourceService;
import com.ruoyi.web.modules.diagnosis.support.MedicalResourceBatchIds;
import com.ruoyi.web.modules.diagnosis.support.MedicalResourceTypeValidator;
import com.ruoyi.web.modules.diagnosis.support.MedicalResourceValidation;

/**
 * 畜牧医疗资源管理
 */
@RestController
@RequestMapping("/diseaseTreatment/medicalResource")
public class BizMedicalResourceController extends BaseController
{
    @Autowired
    private IBizMedicalResourceService bizMedicalResourceService;

    @Autowired
    private MedicalResourceTypeValidator medicalResourceTypeValidator;

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

    @PreAuthorize("@ss.hasPermi('diseaseTreatment:medicalResource:list')")
    @GetMapping("/townOptions")
    public AjaxResult townOptions()
    {
        return success(bizMedicalResourceService.listTownOptions());
    }

    @PreAuthorize("@ss.hasPermi('diseaseTreatment:medicalResource:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizMedicalResourceService.selectBizMedicalResourceById(id));
    }

    @PreAuthorize("@ss.hasPermi('diseaseTreatment:medicalResource:add')")
    @Log(title = "畜牧医疗资源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizMedicalResource row)
    {
        row.setCreateBy(getUsername());
        return toAjax(bizMedicalResourceService.insertBizMedicalResource(row));
    }

    @PreAuthorize("@ss.hasPermi('diseaseTreatment:medicalResource:edit')")
    @Log(title = "畜牧医疗资源", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizMedicalResource row)
    {
        row.setUpdateBy(getUsername());
        return toAjax(bizMedicalResourceService.updateBizMedicalResource(row));
    }

    @PreAuthorize("@ss.hasPermi('diseaseTreatment:medicalResource:remove')")
    @Log(title = "畜牧医疗资源", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bizMedicalResourceService.deleteBizMedicalResourceByIds(ids, getUsername()));
    }

    @PreAuthorize("@ss.hasPermi('diseaseTreatment:medicalResource:publish')")
    @Log(title = "畜牧医疗资源批量发布", businessType = BusinessType.UPDATE)
    @PostMapping("/publish/batch")
    public AjaxResult publishBatch(@RequestBody MedicalResourceBatchIdsBody body)
    {
        String ids = body != null ? body.getIds() : null;
        return toAjax(bizMedicalResourceService.publishByIds(MedicalResourceBatchIds.parse(ids)));
    }

    @PreAuthorize("@ss.hasPermi('diseaseTreatment:medicalResource:publish')")
    @Log(title = "畜牧医疗资源发布", businessType = BusinessType.UPDATE)
    @PostMapping("/publish/{id}")
    public AjaxResult publish(@PathVariable Long id)
    {
        return toAjax(bizMedicalResourceService.publish(id));
    }

    @PreAuthorize("@ss.hasPermi('diseaseTreatment:medicalResource:offline')")
    @Log(title = "畜牧医疗资源批量下架", businessType = BusinessType.UPDATE)
    @PostMapping("/offline/batch")
    public AjaxResult offlineBatch(@RequestBody MedicalResourceBatchIdsBody body)
    {
        String ids = body != null ? body.getIds() : null;
        return toAjax(bizMedicalResourceService.offlineByIds(MedicalResourceBatchIds.parse(ids)));
    }

    @PreAuthorize("@ss.hasPermi('diseaseTreatment:medicalResource:offline')")
    @Log(title = "畜牧医疗资源下架", businessType = BusinessType.UPDATE)
    @PostMapping("/offline/{id}")
    public AjaxResult offline(@PathVariable Long id)
    {
        return toAjax(bizMedicalResourceService.offline(id));
    }

    @PreAuthorize("@ss.hasPermi('diseaseTreatment:medicalResource:assignAccount')")
    @Log(title = "畜牧医疗资源批量分配账号", businessType = BusinessType.INSERT)
    @PostMapping("/assignAccount/batch")
    public AjaxResult assignAccountBatch(@RequestBody MedicalResourceBatchIdsBody body)
    {
        String ids = body != null ? body.getIds() : null;
        return toAjax(bizMedicalResourceService.assignAccountByIds(
                MedicalResourceBatchIds.parse(ids), getUsername()));
    }

    @PreAuthorize("@ss.hasPermi('diseaseTreatment:medicalResource:assignAccount')")
    @Log(title = "畜牧医疗资源分配账号", businessType = BusinessType.INSERT)
    @PostMapping("/assignAccount/{id}")
    public AjaxResult assignAccount(@PathVariable Long id)
    {
        return toAjax(bizMedicalResourceService.assignAccount(id, getUsername()));
    }
}
