package com.ruoyi.web.controller.warehouse.feeagreement;

import java.util.List;

import com.ruoyi.feeagreement.domain.TWarehouseFeeAgreementTypeBusiness;
import com.ruoyi.feeagreement.service.ITWarehouseFeeAgreementTypeBusinessService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
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 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.feeagreement.domain.TWarehouseFeeAgreementType;
import com.ruoyi.feeagreement.service.ITWarehouseFeeAgreementTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 费用协议主表Controller
 * 
 * @author dongyongwei
 * @date 2021-08-04
 */
@RestController
@RequestMapping("/feeagreement/WarehouseFeeAgreementType")
public class TWarehouseFeeAgreementTypeController extends BaseController
{
    @Autowired
    private ITWarehouseFeeAgreementTypeService tWarehouseFeeAgreementTypeService;

    @Autowired
    private ITWarehouseFeeAgreementTypeBusinessService tWarehouseFeeAgreementTypeBusinessService;

    /**
     * 查询费用协议主表列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TWarehouseFeeAgreementType tWarehouseFeeAgreementType)
    {
        startPage();
        List<TWarehouseFeeAgreementType> list = tWarehouseFeeAgreementTypeService.selectTWarehouseFeeAgreementTypeList(tWarehouseFeeAgreementType);
        return getDataTable(list);
    }

    /**
     * 导出费用协议主表列表
     */
    @Log(title = "费用协议主表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TWarehouseFeeAgreementType tWarehouseFeeAgreementType)
    {
        List<TWarehouseFeeAgreementType> list = tWarehouseFeeAgreementTypeService.selectTWarehouseFeeAgreementTypeList(tWarehouseFeeAgreementType);
        ExcelUtil<TWarehouseFeeAgreementType> util = new ExcelUtil<TWarehouseFeeAgreementType>(TWarehouseFeeAgreementType.class);
        return util.exportExcel(list, "WarehouseFeeAgreementType");
    }

    /**
     * 获取费用协议主表详细信息
     */
    @GetMapping(value = "/{fId}")
    public AjaxResult getInfo(@PathVariable("fId") Long fId)
    {
        TWarehouseFeeAgreementType tWarehouseFeeAgreementType = tWarehouseFeeAgreementTypeService.selectTWarehouseFeeAgreementTypeById(fId);
        TWarehouseFeeAgreementTypeBusiness tWarehouseFeeAgreementTypeBusinessQuery = new TWarehouseFeeAgreementTypeBusiness();
        tWarehouseFeeAgreementTypeBusinessQuery.setfWarehouseFeeAgreementTypeFId(fId);
        List<TWarehouseFeeAgreementTypeBusiness> tWarehouseFeeAgreementTypeBusinessList = tWarehouseFeeAgreementTypeBusinessService.selectTWarehouseFeeAgreementTypeBusinessList(tWarehouseFeeAgreementTypeBusinessQuery);
        if(tWarehouseFeeAgreementTypeBusinessList.size()>0){
            tWarehouseFeeAgreementType.settWarehouseFeeAgreementTypeBusinessList(tWarehouseFeeAgreementTypeBusinessList);
        }
        return AjaxResult.success(tWarehouseFeeAgreementType);
    }

    /**
     * 新增费用协议主表
     */
    @Log(title = "费用协议主表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TWarehouseFeeAgreementType tWarehouseFeeAgreementType)
    {
        return toAjax(tWarehouseFeeAgreementTypeService.insertTWarehouseFeeAgreementType(tWarehouseFeeAgreementType));
    }

    /**
     * 新增费用协议主表
     */
    @Log(title = "费用协议主表", businessType = BusinessType.INSERT)
    @PostMapping("/saveAndUpdateTWarehouseFeeAgreementType")
    public AjaxResult saveAndUpdateTWarehouseFeeAgreementType(@RequestBody TWarehouseFeeAgreementType tWarehouseFeeAgreementType)
    {
        //fId存在就更新,不存在就新增
        Long fId =tWarehouseFeeAgreementType.getfId();
        Long fIdReturn = 0L;
        if(fId == null){
            tWarehouseFeeAgreementTypeService.insertTWarehouseFeeAgreementType(tWarehouseFeeAgreementType);
        }else{
            tWarehouseFeeAgreementTypeService.updateTWarehouseFeeAgreementType(tWarehouseFeeAgreementType);
        }
        fIdReturn = tWarehouseFeeAgreementType.getfId();
        List<TWarehouseFeeAgreementTypeBusiness> tWarehouseFeeAgreementTypeBusinessList = tWarehouseFeeAgreementType.gettWarehouseFeeAgreementTypeBusinessList();
        if(tWarehouseFeeAgreementTypeBusinessList!= null){
            for (int i = 0; i < tWarehouseFeeAgreementTypeBusinessList.size(); i++) {
                TWarehouseFeeAgreementTypeBusiness tWarehouseFeeAgreementTypeBusiness = tWarehouseFeeAgreementTypeBusinessList.get(i);
                tWarehouseFeeAgreementTypeBusiness.setfWarehouseFeeAgreementTypeFId(fIdReturn);
                if(tWarehouseFeeAgreementTypeBusiness.getfId()!=null){
                    //update
                    tWarehouseFeeAgreementTypeBusinessService.updateTWarehouseFeeAgreementTypeBusiness(tWarehouseFeeAgreementTypeBusiness);
                }else{
                    //insert
                    tWarehouseFeeAgreementTypeBusinessService.insertTWarehouseFeeAgreementTypeBusiness(tWarehouseFeeAgreementTypeBusiness);
                }
            }
        }
        return AjaxResult.success(fIdReturn);
    }



    /**
     * 修改费用协议主表
     */
    @Log(title = "费用协议主表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TWarehouseFeeAgreementType tWarehouseFeeAgreementType)
    {
        return toAjax(tWarehouseFeeAgreementTypeService.updateTWarehouseFeeAgreementType(tWarehouseFeeAgreementType));
    }

    /**
     * 删除费用协议主表
     */
    @Log(title = "费用协议主表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fIds}")
    public AjaxResult remove(@PathVariable Long[] fIds)
    {
        for (int i = 0; i < fIds.length; i++) {
            Long fId =fIds[i];
            TWarehouseFeeAgreementType tWarehouseFeeAgreementType = tWarehouseFeeAgreementTypeService.selectTWarehouseFeeAgreementTypeById(fId);
            tWarehouseFeeAgreementType.setfDelFlag(1);
            tWarehouseFeeAgreementTypeService.updateTWarehouseFeeAgreementType(tWarehouseFeeAgreementType);
        }
        return AjaxResult.success();
    }
}
