package cn.ezhizao.project.business.controller;

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.BizOutsourcedOrderDetail;
import cn.ezhizao.project.business.domain.BizOutsourcedOrderDetailProcess;
import cn.ezhizao.project.business.domain.BizReturnReceiptDetail;
import cn.ezhizao.project.business.service.IBizReturnReceiptDetailService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 外协单从Controller
 *
 * @author 庄德政
 * date    2024-05-06
 */
@RestController
@RequestMapping("/business/returnReceiptDetail")
public class BizReturnReceiptDetailController extends BaseController
{
    @Resource
    private IBizReturnReceiptDetailService bizReturnReceiptDetailService;

    /**
     * 查询外协单从列表
     */
    @PreAuthorize("@ss.hasPermi('business:returnReceiptDetail:list')")
    @Log(title = "外协收回单明细查询详细信息", businessType = BusinessType.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BizReturnReceiptDetail bizReturnReceiptDetail) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizReturnReceiptDetail);
        startPage();
        List<BizReturnReceiptDetail> list = bizReturnReceiptDetailService.getList(bizReturnReceiptDetail);
        return getDataTable(list);
    }

    /**
     * 导出外协单从列表
     */
    @Log(title = "外协收回单明细导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizReturnReceiptDetail bizReturnReceiptDetail) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizReturnReceiptDetail);
        List<BizReturnReceiptDetail> list = bizReturnReceiptDetailService.getList(bizReturnReceiptDetail);
        ExcelUtil<BizReturnReceiptDetail> util = new ExcelUtil<>(BizReturnReceiptDetail.class);
        util.exportExcel(response, list, "外协单从数据");
    }
    //新增收回单明细
    @Log(title = "外协收回单明细保存", businessType = BusinessType.UPDATE)
    @PostMapping
    public AjaxResult save(@RequestBody List<BizReturnReceiptDetail> bizReturnReceiptDetail)
    {
        List<BizReturnReceiptDetail> oldList = bizReturnReceiptDetailService.query().eq("outsource_detail_id", bizReturnReceiptDetail.get(0).getOutsourceDetailId()).list();
        saveOrUpdateBatch(bizReturnReceiptDetailService, bizReturnReceiptDetail, oldList);
        return success();
    }

    /**
     * 获取外协单从详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:returnReceiptDetail:query')")
    @Log(title = "外协收回单明细查询详细信息", businessType = BusinessType.SELECT)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizReturnReceiptDetailService.getById(id));
    }
    @Log(title = "外协收回单通过发出单明细id查询收回单明细", businessType = BusinessType.SELECT)
    @GetMapping("/getReturnReceiptDetail")
    public AjaxResult getReturnReceiptDetail(BizOutsourcedOrderDetail bizOutsourcedOrderDetail)
    {
        return success(bizReturnReceiptDetailService.query().eq("outsource_detail_id", bizOutsourcedOrderDetail.getId()).list());
    }
    @Log(title = "外协收回单明细删除", businessType = BusinessType.SELECT)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        List<BizReturnReceiptDetail> list = bizReturnReceiptDetailService.query().eq("outsource_detail_id", id).list();
        return toAjax(bizReturnReceiptDetailService.removeBatchByIds(list));
    }
}
