package com.ruoyi.project.task.controller;

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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.check.domain.TCheckCheckpoints;
import com.ruoyi.project.check.domain.TCheckRepairpoints;
import com.ruoyi.project.check.mapper.TCheckRepairpointsMapper;
import com.ruoyi.project.task.domain.TTaskInspection;
import com.ruoyi.project.task.mapper.TTaskRepairMapper;
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.project.task.domain.TTaskRepair;
import com.ruoyi.project.task.service.ITTaskRepairService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 维修任务Controller
 *
 * @author ruoyi
 * @date 2022-11-30
 */
@RestController
@RequestMapping("/task/repair")
public class TTaskRepairController extends BaseController
{
    @Autowired
    private ITTaskRepairService tTaskRepairService;

    @Autowired
    private TCheckRepairpointsMapper tTaskRepairpointsMapper;

    /**
     * 查询维修任务列表
     */
    @PreAuthorize("@ss.hasPermi('task:repair:list')")
    @GetMapping("/list")
    public TableDataInfo list(TTaskRepair tTaskRepair)
    {
        startPage();
        List<TTaskRepair> list = tTaskRepairService.selectTTaskRepairList(tTaskRepair);
        list.forEach(item -> {
            if (StringUtils.isNotEmpty(item.getTaskDoneNum()) && StringUtils.isNotEmpty(item.getTaskNum())) {
                item.setPercentage(new BigDecimal(item.getTaskDoneNum()).divide(new BigDecimal(item.getTaskNum()), 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).doubleValue());
            } else {
                item.setPercentage(0.0);
            }
            if (item.getEndTime() != null) {
                if (item.getEndTime().before(new Date()) && StringUtils.isNotEmpty(item.getTaskNum()) && !item.getTaskNum().equals(item.getTaskDoneNum())) {
                    item.setTimeOut("是");
                } else {
                    item.setTimeOut("否");
                }
            }
        });
        return getDataTable(list);
    }

    /**
     * 导出维修任务列表
     */
    @PreAuthorize("@ss.hasPermi('task:repair:export')")
    @Log(title = "维修任务导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TTaskRepair tTaskRepair)
    {
        List<TTaskRepair> list = tTaskRepairService.selectTTaskRepairList(tTaskRepair);
        ExcelUtil<TTaskRepair> util = new ExcelUtil<TTaskRepair>(TTaskRepair.class);
        util.exportExcel(response, list, "维修任务数据");
    }

    /**
     * 获取维修任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('task:repair:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tTaskRepairService.selectTTaskRepairById(id));
    }

    /**
     * 新增维修任务
     */
    @PreAuthorize("@ss.hasPermi('task:repair:add')")
    @Log(title = "维修任务新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TTaskRepair tTaskRepair)
    {
        tTaskRepair.setCreaterCode(getUserId());
        return toAjax(tTaskRepairService.insertTTaskRepair(tTaskRepair));
    }

    /**
     * 修改维修任务
     */
    @PreAuthorize("@ss.hasPermi('task:repair:edit')")
    @Log(title = "维修任务修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TTaskRepair tTaskRepair)
    {
        tTaskRepair.setUpdaterCode(getUserId());
        return toAjax(tTaskRepairService.updateTTaskRepair(tTaskRepair));
    }

    /**
     * 删除维修任务
     */
    @PreAuthorize("@ss.hasPermi('task:repair:remove')")
    @Log(title = "维修任务删除", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tTaskRepairService.deleteTTaskRepairByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('task:repair:edit')")
    @Log(title = "维修任务分配", businessType = BusinessType.UPDATE)
    @PutMapping("/{ids}")
    public AjaxResult divide(@PathVariable Long[] ids) {
        QueryWrapper<TCheckRepairpoints> wrapper = new QueryWrapper<>();
        for (Long id : ids) {
            wrapper.clear();
            wrapper.eq("repair_id", id);
            Integer count = tTaskRepairpointsMapper.selectCount(wrapper);
            if (count ==0) {
                continue;
            }
            TTaskRepair tTaskRepair = new TTaskRepair();
            tTaskRepair.setId(id);
            tTaskRepair.setTaskNum(count.toString());
            tTaskRepair.setTaskUndoneNum(count.toString());
            tTaskRepair.setStatus("1");
            tTaskRepairService.updateTTaskRepair(tTaskRepair);
        }
        return AjaxResult.success();
    }
}
