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.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ISysUserService;
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.CmcCar;
import com.ruoyi.oa.service.ICmcCarService;
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/car")
public class CmcCarController extends BaseController
{
    @Autowired
    private ICmcCarService cmcCarService;

    @Autowired
    private ISysUserService userService;
    /**
     * 查询cmc车辆信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcCar cmcCar)
    {
        startPage();
        List<CmcCar> list = cmcCarService.selectCmcCarList(cmcCar);
        return getDataTable(list);
    }

    /**
     * 导出cmc车辆信息列表
     */
    @Log(title = "cmc车辆信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmcCar cmcCar)
    {
        List<CmcCar> list = cmcCarService.selectCmcCarList(cmcCar);
        ExcelUtil<CmcCar> util = new ExcelUtil<CmcCar>(CmcCar.class);
        util.exportExcel(response, list, "cmc车辆信息数据");
    }

    /**
     * 获取cmc车辆信息详细信息
     */
    @GetMapping(value = "/{carId}")
    public AjaxResult getInfo(@PathVariable("carId") Integer carId)
    {
        JSONObject jsonObject = new JSONObject();
        CmcCar cmcCar = cmcCarService.selectCmcCarByCarId(carId);
        jsonObject.put("carId", cmcCar.getCarId());
        jsonObject.put("licensePlate", cmcCar.getLicensePlate());
        jsonObject.put("driver", cmcCar.getDriver() != null ? userService.selectUserById(cmcCar.getDriver()).getNickName() : "");
        jsonObject.put("brand", cmcCar.getBrand());
        jsonObject.put("series", cmcCar.getSeries());
        jsonObject.put("cost", cmcCar.getCost());
        jsonObject.put("acquisitionTime", cmcCar.getAcquisitionTime());
        jsonObject.put("expectLife", cmcCar.getExpectLife());
        jsonObject.put("expectKm", cmcCar.getExpectKm());
        jsonObject.put("isRent", cmcCar.getIsRent());
        jsonObject.put("dayCost", cmcCar.getDayCost());
        return success(jsonObject);
    }

    /**
     * 新增cmc车辆信息
     */
    @Log(title = "cmc车辆信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmcCar cmcCar)
    {
        if (cmcCar.getAcquisitionTime() != null && cmcCar.getCost() != null && cmcCar.getDayCost() != null) {
            // 年数总和法折旧
            int acquisitionYear = Integer.parseInt(new SimpleDateFormat("yyyy").format(cmcCar.getAcquisitionTime()));
            int life = cmcCar.getExpectLife() == null ? 0 : cmcCar.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(cmcCar.getCost());
                cmcCar.setDayCost(estimateCost);
            }
        }
        return toAjax(cmcCarService.insertCmcCar(cmcCar));
    }

    /**
     * 修改cmc车辆信息
     */
    @Log(title = "cmc车辆信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmcCar cmcCar)
    {
        return toAjax(cmcCarService.updateCmcCar(cmcCar));
    }

    /**
     * 删除cmc车辆信息
     */
    @Log(title = "cmc车辆信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{carIds}")
    public AjaxResult remove(@PathVariable Long[] carIds)
    {
        return toAjax(cmcCarService.deleteCmcCarByCarIds(carIds));
    }
}
