package com.ruoyi.web.controller.oa;

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

import com.ruoyi.common.utils.DateUtils;
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.oa.domain.CmcDevice;
import com.ruoyi.oa.service.ICmcDeviceService;
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/device")
public class CmcDeviceController extends BaseController
{
    @Autowired
    private ICmcDeviceService cmcDeviceService;

    /**
     * 查询cmc设备信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcDevice cmcDevice)
    {
        startPage();
        List<CmcDevice> list = cmcDeviceService.selectCmcDeviceList(cmcDevice);
        return getDataTable(list);
    }

    /**
     * 导出cmc设备信息列表
     */
    @Log(title = "cmc设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmcDevice cmcDevice)
    {
        List<CmcDevice> list = cmcDeviceService.selectCmcDeviceList(cmcDevice);
        ExcelUtil<CmcDevice> util = new ExcelUtil<CmcDevice>(CmcDevice.class);
        util.exportExcel(response, list, "cmc设备信息数据");
    }

    /**
     * 获取cmc设备信息详细信息
     */
    @GetMapping(value = "/{deviceId}")
    public AjaxResult getInfo(@PathVariable("deviceId") Integer deviceId)
    {
        return success(cmcDeviceService.selectCmcDeviceByDeviceId(deviceId));
    }

    /**
     * 新增cmc设备信息
     */
    @Log(title = "cmc设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmcDevice cmcDevice)
    {
        if (cmcDevice.getAcquisitionTime() != null && cmcDevice.getCost() != null && cmcDevice.getDayCost() != null) {
            // 年数总和法折旧
            int acquisitionYear = Integer.parseInt(new SimpleDateFormat("yyyy").format(cmcDevice.getAcquisitionTime()));
            int life = cmcDevice.getExpectLife() == null ? 0 : cmcDevice.getExpectLife();
            int total = 0;
            for (int i = 0; i < life + 1; i++)
                total += i;
            if (total > 0) {
                int currentYear = Integer.parseInt(new SimpleDateFormat("yyyy").format(DateUtils.getNowDate()));
                int num = life - (currentYear - acquisitionYear) + 1;
                BigDecimal estimateCost = new BigDecimal(num  * 0.95 * 10000 / total / 365).multiply(cmcDevice.getCost());
                cmcDevice.setDayCost(estimateCost);
            }
        }
        return toAjax(cmcDeviceService.insertCmcDevice(cmcDevice));
    }

    /**
     * 修改cmc设备信息
     */
    @Log(title = "cmc设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmcDevice cmcDevice)
    {
        return toAjax(cmcDeviceService.updateCmcDevice(cmcDevice));
    }

    /**
     * 删除cmc设备信息
     */
    @Log(title = "cmc设备信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deviceIds}")
    public AjaxResult remove(@PathVariable Long[] deviceIds)
    {
        return toAjax(cmcDeviceService.deleteCmcDeviceByDeviceIds(deviceIds));
    }
}
