package com.ruoyi.web.controller.oa;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
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.oa.domain.CmcBorrow;
import com.ruoyi.oa.service.ICmcBorrowService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * cmc借款申请Controller
 * 
 * @author cmc
 * @date 2024-04-07
 */
@RestController
@RequestMapping("/oa/borrow")
public class CmcBorrowController extends BaseController
{
    @Autowired
    private ICmcBorrowService cmcBorrowService;

    /**
     * 查询cmc借款申请列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcBorrow cmcBorrow)
    {
        startPage();
        List<CmcBorrow> list = cmcBorrowService.selectCmcBorrowList(cmcBorrow);
        return getDataTable(list);
    }

    /**
     * 导出cmc借款申请列表
     */
    @Log(title = "cmc借款申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmcBorrow cmcBorrow)
    {
        List<CmcBorrow> list = cmcBorrowService.selectCmcBorrowList(cmcBorrow);
        ExcelUtil<CmcBorrow> util = new ExcelUtil<CmcBorrow>(CmcBorrow.class);
        util.exportExcel(response, list, "cmc借款申请数据");
    }

    /**
     * 获取cmc借款申请详细信息
     */
    @GetMapping(value = "/{borrowId}")
    public AjaxResult getInfo(@PathVariable("borrowId") String borrowId)
    {
        return success(cmcBorrowService.selectCmcBorrowByBorrowId(borrowId));
    }

    /**
     * 新增cmc借款申请
     */
    @Log(title = "cmc借款申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmcBorrow cmcBorrow)
    {
        cmcBorrow.setApplyDept(getLoginUser().getDeptId());
        return toAjax(cmcBorrowService.insertCmcBorrow(cmcBorrow));
    }

    /**
     * 修改cmc借款申请
     */
    @Log(title = "cmc借款申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmcBorrow cmcBorrow)
    {
        if (cmcBorrow.getDeptComment() != null && cmcBorrow.getManagerComment() == null) {
            cmcBorrow.setDeptUserId(getLoginUser().getUserId());
            cmcBorrow.setDeptTime(new Date());
        }
        if (cmcBorrow.getManagerComment() != null && cmcBorrow.getZjlComment() == null) {
            cmcBorrow.setManagerUserId(getLoginUser().getUserId());
            cmcBorrow.setManagerTime(new Date());
        }
        if (cmcBorrow.getZjlComment() != null && cmcBorrow.getDszComment() == null) {
            cmcBorrow.setZjlUserId(getLoginUser().getUserId());
            cmcBorrow.setZjlTime(new Date());
        }
        if (cmcBorrow.getDszComment() != null && cmcBorrow.getCwComment() == null) {
            cmcBorrow.setDszUserId(getLoginUser().getUserId());
            cmcBorrow.setDszTime(new Date());
        }
        if (cmcBorrow.getUnionComment() != null && cmcBorrow.getCwComment() == null) {
            cmcBorrow.setUnionUserId(getLoginUser().getUserId());
            cmcBorrow.setUnionTime(new Date());
        }
        if (cmcBorrow.getLendTime() != null) {
            cmcBorrow.setCwUserId(getLoginUser().getUserId());
        }
        return toAjax(cmcBorrowService.updateCmcBorrow(cmcBorrow));
    }

    /**
     * 删除cmc借款申请
     */
    @Log(title = "cmc借款申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{borrowIds}")
    public AjaxResult remove(@PathVariable String[] borrowIds)
    {
        return success(cmcBorrowService.deleteCmcBorrowByBorrowIds(borrowIds));
    }
}
