package com.ruoyi.web.controller.oa;

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

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.oa.domain.*;
import com.ruoyi.oa.service.ICmcCarService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
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.oa.service.ICmcCarApprovalService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * cmc用车审批Controller
 * 
 * @author cmc
 * @date 2024-02-23
 */
@RestController
@RequestMapping("/oa/carApproval")
public class CmcCarApprovalController extends BaseController
{
    @Autowired
    private ICmcCarApprovalService cmcCarApprovalService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ICmcCarService cmcCarService;

    /**
     * 查询cmc用车审批列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcCarApproval cmcCarApproval)
    {
        startPage();
        List<CmcCarApproval> list = cmcCarApprovalService.selectCmcCarApprovalList(cmcCarApproval);
        return getDataTable(list);
    }

    /**
     * 导出cmc用车审批列表
     */
    @Log(title = "cmc用车审批", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmcCarApproval cmcCarApproval)
    {
        List<CmcCarApproval> list = cmcCarApprovalService.selectCmcCarApprovalList(cmcCarApproval);
        ExcelUtil<CmcCarApproval> util = new ExcelUtil<CmcCarApproval>(CmcCarApproval.class);
        util.exportExcel(response, list, "cmc用车审批数据");
    }

    /**
     * 获取cmc用车审批详细信息
     */
    @GetMapping(value = "/{carApplyId}")
    public AjaxResult getInfo(@PathVariable("carApplyId") String carApplyId)
    {
        JSONObject formData = new JSONObject();
        CmcCarApproval cmcCarApproval = cmcCarApprovalService.selectCmcCarApprovalByCarApplyId(carApplyId);
        if (cmcCarApproval != null) {
            formData.put("user", userService.selectUserById(cmcCarApproval.getApplier()).getNickName());
            formData.put("deptName", deptService.selectDeptById(userService.selectUserById(cmcCarApproval.getApplier()).getDeptId()).getDeptName());
            formData.put("applyDate", cmcCarApproval.getApplyDate());
            formData.put("beginDate", cmcCarApproval.getBeginDate());
            formData.put("endDate", cmcCarApproval.getEndDate());
            formData.put("days", cmcCarApproval.getDays());
            formData.put("passengers", cmcCarApproval.getPassengers());
            formData.put("projectNumber", cmcCarApproval.getProjectId());
            formData.put("applyReason", cmcCarApproval.getApplyReason());
            formData.put("deptUser", cmcCarApproval.getDeptUserId());
            formData.put("deptComment", cmcCarApproval.getDeptComment());
            formData.put("managerUser", cmcCarApproval.getManagerUserId());
            formData.put("managerComment", cmcCarApproval.getManagerComment());
            formData.put("unionUser", cmcCarApproval.getUnionUserId());
            formData.put("unionComment", cmcCarApproval.getUnionComment());
            formData.put("dispatcher", cmcCarApproval.getDispatcher());
            formData.put("dispatchComment", cmcCarApproval.getDispatchComment());
            formData.put("cars", cmcCarApproval.getCars());
            formData.put("drivers", cmcCarApproval.getDrivers());
        }
        return success(formData);
    }

    /**
     * 新增cmc用车审批
     */
    @Log(title = "cmc用车审批", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmcCarApproval cmcCarApproval)
    {
        return toAjax(cmcCarApprovalService.insertCmcCarApproval(cmcCarApproval));
    }

    /**
     * 提交用车审批表
     */
    @Log(title = "用车审批表", businessType = BusinessType.INSERT)
    @PostMapping( "/submit")
    public AjaxResult submit(@RequestParam("form")String formData)
    {
        JSONObject formDataJson = JSONObject.parse(formData);
        CmcCarApproval cmcCarApproval = new CmcCarApproval();
        cmcCarApproval.setCarApplyId(formDataJson.getString("formId"));
        cmcCarApproval.setApplier(getLoginUser().getUserId());
        cmcCarApproval.setApplyDate(DateUtils.getNowDate());
        cmcCarApproval.setProjectId(formDataJson.getString("projectNumber"));
        cmcCarApproval.setApplyReason(formDataJson.getString("applyReason"));
        cmcCarApproval.setPassengers(formDataJson.getInteger("passengers"));
        cmcCarApproval.setBeginDate(formDataJson.getDate("beginDate"));
        cmcCarApproval.setEndDate(formDataJson.getDate("endDate"));
        cmcCarApproval.setDays(formDataJson.getLong("days"));
        cmcCarApprovalService.insertCmcCarApproval(cmcCarApproval);
        return AjaxResult.success("提交用车审批表成功");
    }

    /**
     * 修改cmc用车审批
     */
    @Log(title = "cmc用车审批", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmcCarApproval cmcCarApproval)
    {
        return toAjax(cmcCarApprovalService.updateCmcCarApproval(cmcCarApproval));
    }

    /**
     * 修改用车审批表
     */
    @Log(title = "用车审批表", businessType = BusinessType.UPDATE)
    @PutMapping( "/modify")
    public AjaxResult modify(@RequestParam("form")String formData)
    {
        JSONObject formDataJson = JSONObject.parse(formData);
        CmcCarApproval cmcCarApproval = cmcCarApprovalService.selectCmcCarApprovalByCarApplyId(formDataJson.getString("carApplyId"));
        if (formDataJson.getString("deptComment") != null) {
            cmcCarApproval.setDeptUserId(getLoginUser().getUserId());
            cmcCarApproval.setDeptComment(formDataJson.getString("deptComment"));
        }
        if (formDataJson.getString("managerComment") != null) {
            cmcCarApproval.setManagerUserId(getLoginUser().getUserId());
            cmcCarApproval.setManagerComment(formDataJson.getString("managerComment"));
        }
        if (formDataJson.getString("unionComment") != null) {
            cmcCarApproval.setUnionUserId(getLoginUser().getUserId());
            cmcCarApproval.setUnionComment(formDataJson.getString("unionComment"));
        }
        if (formDataJson.getString("dispatchComment") != null) {
            cmcCarApproval.setDispatcher(getLoginUser().getUserId());
            cmcCarApproval.setDispatchComment(formDataJson.getString("dispatchComment"));
        }
        if (formDataJson.getString("cars") != null && formDataJson.getString("drivers") != null) {
            cmcCarApproval.setDispatcher(getLoginUser().getUserId());
            String carString = formDataJson.getString("cars").substring(1, formDataJson.getString("cars").length() - 1);
            cmcCarApproval.setCars(carString);
            cmcCarApproval.setDrivers(formDataJson.getString("drivers").substring(1, formDataJson.getString("drivers").length() - 1));
            String[] cars = carString.split(",");
            BigDecimal estimateCost = new BigDecimal(0);
            for (String car : cars) {
                CmcCar cmcCar = cmcCarService.selectCmcCarByCarId(Integer.parseInt(car));
                if (cmcCar.getDayCost() != null)
                    estimateCost = estimateCost.add(cmcCar.getDayCost().multiply(new BigDecimal(cmcCarApproval.getDays())));
            }
            cmcCarApproval.setEstimateCost(estimateCost);
        }
        cmcCarApprovalService.updateCmcCarApproval(cmcCarApproval);
        return AjaxResult.success("修改用车审批表成功");
    }

    /**
     * 删除cmc用车审批
     */
    @Log(title = "cmc用车审批", businessType = BusinessType.DELETE)
	@DeleteMapping("/{carApplyIds}")
    public AjaxResult remove(@PathVariable String[] carApplyIds)
    {
        return toAjax(cmcCarApprovalService.deleteCmcCarApprovalByCarApplyIds(carApplyIds));
    }
}
