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.ICmcDeviceService;
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.domain.CmcDeviceApproval;
import com.ruoyi.oa.service.ICmcDeviceApprovalService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * cmc设备审批Controller
 * 
 * @author cmc
 * @date 2024-03-05
 */
@RestController
@RequestMapping("/oa/deviceApproval")
public class CmcDeviceApprovalController extends BaseController
{
    @Autowired
    private ICmcDeviceApprovalService cmcDeviceApprovalService;

    @Autowired
    private ICmcDeviceService cmcDeviceService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询cmc设备审批列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcDeviceApproval cmcDeviceApproval)
    {
        startPage();
        List<CmcDeviceApproval> list = cmcDeviceApprovalService.selectCmcDeviceApprovalList(cmcDeviceApproval);
        return getDataTable(list);
    }

    /**
     * 导出cmc设备审批列表
     */
    @Log(title = "cmc设备审批", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmcDeviceApproval cmcDeviceApproval)
    {
        List<CmcDeviceApproval> list = cmcDeviceApprovalService.selectCmcDeviceApprovalList(cmcDeviceApproval);
        ExcelUtil<CmcDeviceApproval> util = new ExcelUtil<CmcDeviceApproval>(CmcDeviceApproval.class);
        util.exportExcel(response, list, "cmc设备审批数据");
    }

    /**
     * 获取cmc设备审批详细信息
     */
    @GetMapping(value = "/{deviceApplyId}")
    public AjaxResult getInfo(@PathVariable("deviceApplyId") String deviceApplyId)
    {
        JSONObject formData = new JSONObject();
        CmcDeviceApproval cmcDeviceApproval = cmcDeviceApprovalService.selectCmcDeviceApprovalByDeviceApplyId(deviceApplyId);
        if (cmcDeviceApproval != null) {
            formData.put("user", userService.selectUserById(cmcDeviceApproval.getApplier()).getNickName());
            formData.put("deptName", deptService.selectDeptById(userService.selectUserById(cmcDeviceApproval.getApplier()).getDeptId()).getDeptName());
            formData.put("applyDate", cmcDeviceApproval.getApplyDate());
            formData.put("beginDate", cmcDeviceApproval.getBeginDate());
            formData.put("endDate", cmcDeviceApproval.getEndDate());
            formData.put("days", cmcDeviceApproval.getDays());
            formData.put("projectNumber", cmcDeviceApproval.getProjectId());
            formData.put("applyReason", cmcDeviceApproval.getApplyReason());
            formData.put("deptUser", cmcDeviceApproval.getDeptUserId());
            formData.put("deptComment", cmcDeviceApproval.getDeptComment());
            formData.put("managerUser", cmcDeviceApproval.getManagerUserId());
            formData.put("managerComment", cmcDeviceApproval.getManagerComment());
            formData.put("dispatcher", cmcDeviceApproval.getDispatcher());
            formData.put("dispatchComment", cmcDeviceApproval.getDispatchComment());
            formData.put("devices", cmcDeviceApproval.getDevices());
        }
        return success(formData);
    }

    /**
     * 新增cmc设备审批
     */
    @Log(title = "cmc设备审批", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmcDeviceApproval cmcDeviceApproval)
    {
        return toAjax(cmcDeviceApprovalService.insertCmcDeviceApproval(cmcDeviceApproval));
    }

    /**
     * 新增cmc设备审批
     */
    @Log(title = "cmc设备审批", businessType = BusinessType.INSERT)
    @PostMapping("/submit")
    public AjaxResult submit(@RequestParam("form")String formData)
    {
        JSONObject formDataJson = JSONObject.parse(formData);
        CmcDeviceApproval cmcDeviceApproval = new CmcDeviceApproval();
        cmcDeviceApproval.setDeviceApplyId(formDataJson.getString("formId"));
        cmcDeviceApproval.setApplier(getLoginUser().getUserId());
        cmcDeviceApproval.setDept(getLoginUser().getDeptId());
        cmcDeviceApproval.setApplyDate(DateUtils.getNowDate());
        cmcDeviceApproval.setProjectId(formDataJson.getString("projectNumber"));
        cmcDeviceApproval.setApplyReason(formDataJson.getString("applyReason"));
        cmcDeviceApproval.setBeginDate(formDataJson.getDate("beginDate"));
        cmcDeviceApproval.setEndDate(formDataJson.getDate("endDate"));
        cmcDeviceApproval.setDays(formDataJson.getLong("days"));
        cmcDeviceApprovalService.insertCmcDeviceApproval(cmcDeviceApproval);
        return AjaxResult.success("提交设备审批表成功");
    }

    /**
     * 修改cmc设备审批
     */
    @Log(title = "cmc设备审批", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmcDeviceApproval cmcDeviceApproval)
    {
        return toAjax(cmcDeviceApprovalService.updateCmcDeviceApproval(cmcDeviceApproval));
    }

    /**
     * 修改cmc设备审批
     */
    @Log(title = "cmc设备审批", businessType = BusinessType.UPDATE)
    @PutMapping("/modify")
    public AjaxResult modify(@RequestParam("form")String formData)
    {
        JSONObject formDataJson = JSONObject.parse(formData);
        CmcDeviceApproval cmcDeviceApproval = cmcDeviceApprovalService.selectCmcDeviceApprovalByDeviceApplyId(formDataJson.getString("deviceApplyId"));
        if (formDataJson.getString("deptComment") != null) {
            cmcDeviceApproval.setDeptUserId(getLoginUser().getUserId());
            cmcDeviceApproval.setDeptComment(formDataJson.getString("deptComment"));
        }
        if (formDataJson.getString("managerComment") != null) {
            cmcDeviceApproval.setManagerUserId(getLoginUser().getUserId());
            cmcDeviceApproval.setManagerComment(formDataJson.getString("managerComment"));
        }
        if (formDataJson.getDate("returnDate") != null) {
            cmcDeviceApproval.setReturnDate(formDataJson.getDate("returnDate"));
        }
        if (formDataJson.getString("dispatchComment") != null) {
            cmcDeviceApproval.setDispatcher(getLoginUser().getUserId());
            cmcDeviceApproval.setDispatchComment(formDataJson.getString("dispatchComment"));
        }
        if (formDataJson.getString("devices") != null) {
            cmcDeviceApproval.setDispatcher(getLoginUser().getUserId());
            String deviceString = formDataJson.getString("devices").substring(1, formDataJson.getString("devices").length() - 1);
            cmcDeviceApproval.setDevices(deviceString);
            String[] devices = deviceString.split(",");
            BigDecimal estimateCost = new BigDecimal(0);
            for (String device : devices) {
                CmcDevice cmcDevice = cmcDeviceService.selectCmcDeviceByDeviceId(Integer.parseInt(device));
                if (cmcDevice.getDayCost() != null)
                    estimateCost = estimateCost.add(cmcDevice.getDayCost().multiply(new BigDecimal(cmcDeviceApproval.getDays())));
            }
            cmcDeviceApproval.setEstimateCost(estimateCost);
        }
        cmcDeviceApprovalService.updateCmcDeviceApproval(cmcDeviceApproval);
        return AjaxResult.success("修改设备审批表成功");
    }

    /**
     * 删除cmc设备审批
     */
    @Log(title = "cmc设备审批", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deviceApplyIds}")
    public AjaxResult remove(@PathVariable String[] deviceApplyIds)
    {
        return toAjax(cmcDeviceApprovalService.deleteCmcDeviceApprovalByDeviceApplyIds(deviceApplyIds));
    }
}
