package com.ruoyi.web.v1.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.web.v1.model.EntranceBatch;
import com.ruoyi.web.v1.mapper.HarmlessTreatmentMapper;
import com.ruoyi.web.v1.service.IEntranceBatchService;
import com.ruoyi.web.v2.model.JsDivideCircle;
import com.ruoyi.web.v2.service.IJsDivideCircleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.v1.model.HarmlessTreatment;
import com.ruoyi.web.v1.service.IHarmlessTreatmentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 无害化处理Controller
 * 
 * @author coede
 * @date 2025-03-19
 */
@RestController
@RequestMapping("/app/harmlessTreatment")
public class HarmlessTreatmentController extends BaseController
{
    @Autowired
    private IHarmlessTreatmentService harmlessTreatmentService;
    @Autowired
    private IJsDivideCircleService divideCircleService;
    @Autowired
    private IEntranceBatchService entranceBatchService;
    @Autowired
    private HarmlessTreatmentMapper harmlessTreatmentMapper;

    /**
     * 查询无害化处理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HarmlessTreatment harmlessTreatment)
    {
        startPage();
        List<HarmlessTreatment> list = harmlessTreatmentService.selectHarmlessTreatmentList(harmlessTreatment);
        return getDataTable(list);
    }

    @GetMapping("/page")
    public TableDataInfo page()
    {
        List<HarmlessTreatment> harmlessTreatments = harmlessTreatmentMapper.listAll();
        return getDataTable(harmlessTreatments);
    }
    /**
     * 导出无害化处理列表
     */
//    // @PreAuthorize("@ss.hasPermi('app:harmlessTreatment:export')")
    @Log(title = "无害化处理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HarmlessTreatment harmlessTreatment)
    {
        List<HarmlessTreatment> list = harmlessTreatmentService.selectHarmlessTreatmentList(harmlessTreatment);
        ExcelUtil<HarmlessTreatment> util = new ExcelUtil<HarmlessTreatment>(HarmlessTreatment.class);
        util.exportExcel(response, list, "无害化处理数据");
    }

    /**
     * 获取无害化处理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(harmlessTreatmentService.selectHarmlessTreatmentById(id));
    }

    /**
     * 获取无害化处理生猪详情详细信息
     */
    @GetMapping(value = "/detail")
    public AjaxResult getEarInfo(@RequestParam("id") Long id)
    {
        return success(harmlessTreatmentService.selectHarmlessDetail(id));
    }


    /**
     * 新增无害化处理
     */
//    // @PreAuthorize("@ss.hasPermi('app:harmlessTreatment:add')")
    @Log(title = "无害化处理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated  @RequestBody HarmlessTreatment harmlessTreatment)
    {
        //判断全部数量是否超出了入场数量
        int i = harmlessTreatmentService.checkTotalLessThanAmount(harmlessTreatment);
        if(i<0){
            return error("新增无害化处理失败，病死头数、病害头数、途亡头数或急宰头数输入错误");
        }
        harmlessTreatment.setCreateBy(getUsername());
        //存在性判定，已存在入场批次号相同的数据，则更新原有数据，否则新增插入
        if(!ObjectUtils.isEmpty(harmlessTreatment.getEntranceBatchId())) {
            HarmlessTreatment queryEntity = new HarmlessTreatment();
            queryEntity.setEntranceBatchId(harmlessTreatment.getEntranceBatchId());
            List<HarmlessTreatment> list = harmlessTreatmentService.selectHarmlessTreatmentList(queryEntity);
            if(!ObjectUtils.isEmpty(list)) {
                return toAjax(harmlessTreatmentService.updateHarmlessTreatment(harmlessTreatment));
            }
        }
        return toAjax(harmlessTreatmentService.insertHarmlessTreatment(harmlessTreatment));
    }

    /**
     * 修改无害化处理
     */
//    // @PreAuthorize("@ss.hasPermi('app:harmlessTreatment:edit')")
    @Log(title = "无害化处理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody HarmlessTreatment harmlessTreatment)
    {
        //判断全部数量是否超出了入场数量
        int i = harmlessTreatmentService.checkTotalLessThanAmount(harmlessTreatment);
        if(i<0){
            return error("修改无害化处理失败，病死头数、病害头数、途亡头数或急宰头数输入错误");
        }
        //判断是否填写分圈
        EntranceBatch entranceBatch = entranceBatchService.selectEntranceBatchById(harmlessTreatment.getEntranceBatchId());
        long count = divideCircleService.count(new QueryWrapper<JsDivideCircle>().eq("animal_cert_no", entranceBatch.getAnimalCertNo()));
        if (count != 0) {
            return error("已经填写分圈，不可修改无害化处理");
        }
        harmlessTreatment.setUpdateBy(getUsername());
        return toAjax(harmlessTreatmentService.updateHarmlessTreatment(harmlessTreatment));
    }

    /**
     * 删除无害化处理
     */
//    // @PreAuthorize("@ss.hasPermi('app:harmlessTreatment:remove')")
    @Log(title = "无害化处理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        //判断是否填写分圈
        for (Long id : ids) {
            HarmlessTreatment harmlessTreatment = harmlessTreatmentService.selectHarmlessTreatmentById(id);
            EntranceBatch entranceBatch = entranceBatchService.selectEntranceBatchById(harmlessTreatment.getEntranceBatchId());
            long count = divideCircleService.count(new QueryWrapper<JsDivideCircle>().eq("animal_cert_no", entranceBatch.getAnimalCertNo()));
            if (count != 0) {
                return error("已经填写分圈，不可修改无害化处理");
            }
        }
        return toAjax(harmlessTreatmentService.deleteHarmlessTreatmentByIds(ids));
    }
}
