package cn.ezhizao.project.business.controller;

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

import cn.ezhizao.common.utils.poi.ExcelUtil;
import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.framework.web.page.TableDataInfo;
import cn.ezhizao.project.business.domain.BizLot;
import cn.ezhizao.project.business.service.IBizLotService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.ezhizao.project.business.domain.BizLotWaste;
import cn.ezhizao.project.business.service.IBizLotWasteService;


/**
 * 批次废弃Controller
 *
 * @author ezhizao
 * @date 2024-04-02
 */
@RestController
@RequestMapping("/business/lotWaste")
public class BizLotWasteController extends BaseController
{
    @Resource
    private IBizLotWasteService bizLotWasteService;
    @Resource
    private IBizLotService bizLotService;

    /**
     * 查询批次废弃列表
     */
    @PreAuthorize("@ss.hasPermi('business:lotWaste:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizLotWaste bizLotWaste) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizLotWaste);
        startPage();
        List<BizLotWaste> list = bizLotWasteService.getList(bizLotWaste);
        return getDataTable(list);
    }

    /**
     * 导出批次废弃列表
     */
    @PreAuthorize("@ss.hasPermi('business:lotWaste:export')")
    @Log(title = "批次废弃", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizLotWaste bizLotWaste) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizLotWaste);
        List<BizLotWaste> list = bizLotWasteService.getList(bizLotWaste);
        ExcelUtil<BizLotWaste> util = new ExcelUtil<BizLotWaste>(BizLotWaste.class);
        util.exportExcel(response, list, "批次废弃数据");
    }

    /**
     * 获取批次废弃详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:lotWaste:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizLotWasteService.getById(id));
    }

    /**
     * 新增批次废弃
     */
    @PreAuthorize("@ss.hasPermi('business:lotWaste:add')")
    @Log(title = "批次废弃", businessType = BusinessType.INSERT)
    @Transactional
    @PostMapping
    public AjaxResult add(@RequestBody BizLotWaste bizLotWaste) throws NoSuchFieldException, IllegalAccessException
    {
        //批废，修改biz_lot的isWaste字段，再添加
        QueryWrapper<BizLot> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", bizLotWaste.getLotId());
        BizLot bizLot = bizLotService.list(queryWrapper).get(0);
        bizLot.setIsWaste(1);
        bizLotService.updateById(bizLot);
        return toAjax(bizLotWasteService.save(bizLotWaste));
    }

    /**
     * 修改批次废弃
     */
    @PreAuthorize("@ss.hasPermi('business:lotWaste:edit')")
    @Log(title = "批次废弃", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizLotWaste bizLotWaste) throws NoSuchFieldException, IllegalAccessException
    {
        return toAjax(bizLotWasteService.updateById(bizLotWaste));
    }

    /**
     * 删除批次废弃
     */
    @PreAuthorize("@ss.hasPermi('business:lotWaste:remove')")
    @Log(title = "批次废弃", businessType = BusinessType.DELETE)
    @Transactional
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        //取消批废，先修改biz_lot的isWaste字段，再删除
        QueryWrapper<BizLotWaste> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        BizLotWaste lotWaste = bizLotWasteService.getOne(queryWrapper);
        QueryWrapper<BizLot> wrapper = new QueryWrapper<>();
        wrapper.eq("id",lotWaste.getLotId());
        BizLot bizLot = bizLotService.getOne(wrapper);
        bizLot.setIsWaste(0);
        bizLotService.updateById(bizLot);
        return toAjax(bizLotWasteService.removeById(id));
    }
}
