package cn.ezhizao.project.business.processCompletion.controller;

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

import cn.ezhizao.common.utils.poi.ExcelUtil;
import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.framework.web.page.TableDataInfo;
import cn.ezhizao.project.business.processCompletion.domain.BizProcessCompletion;
import cn.ezhizao.project.business.processCompletion.domain.CompleteStatistics;
import cn.ezhizao.project.business.processCompletion.service.IBizProcessCompletionService;
import cn.ezhizao.project.business.processCompletion.service.ICompleteStatisticsService;
import org.springframework.security.access.prepost.PreAuthorize;
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;

/**
 * 企业租户Controller
 *
 * @author ezhizao
 * @date 2024-08-01
 */
@RestController
@RequestMapping("/business/completion")
public class BizProcessCompletionController extends BaseController {
    @Resource
    private IBizProcessCompletionService bizProcessCompletionService;
    @Resource
    private ICompleteStatisticsService completeStatisticsService;

    /**
     * 查询企业租户列表
     */
//    @PreAuthorize("@ss.hasPermi('business:completion:list')")
    @Log(title = "工艺文件下发及时率查询", businessType = BusinessType.EXPORT)
    @GetMapping("/list")
    public TableDataInfo list(BizProcessCompletion bizProcessCompletion) throws NoSuchFieldException, IllegalAccessException {
        setTenantId(bizProcessCompletion);
        startPage();
        List<BizProcessCompletion> list = bizProcessCompletionService.getList(bizProcessCompletion);
        return getDataTable(list);
    }

//    @PreAuthorize("@ss.hasPermi('business:completion:list')")
    @Log(title = "工艺下发及时率图表数据查询", businessType = BusinessType.EXPORT)
    @GetMapping("/listStatistics")
    public TableDataInfo listStatistics(CompleteStatistics completeStatistics) throws NoSuchFieldException, IllegalAccessException {
        List<CompleteStatistics> list = completeStatisticsService.getList(completeStatistics);
        list.forEach(l -> {
            l.setRatio(l.getFinishedNum().equals(0) ? BigDecimal.ZERO : BigDecimal.valueOf(l.getFinishedInTimeNum()).divide(BigDecimal.valueOf(l.getFinishedNum()), 4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)));
        });
        return getDataTable(list);
    }

    /**
     * 导出企业租户列表
     */
//    @PreAuthorize("@ss.hasPermi('business:completion:export')")
    @Log(title = "工艺下发及时率导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizProcessCompletion bizProcessCompletion) throws NoSuchFieldException, IllegalAccessException {
        setTenantId(bizProcessCompletion);
        List<BizProcessCompletion> list = bizProcessCompletionService.getList(bizProcessCompletion);
        ExcelUtil<BizProcessCompletion> util = new ExcelUtil<BizProcessCompletion>(BizProcessCompletion.class);
        util.exportExcel(response, list, "企业租户数据");
    }

    /**
     * 获取企业租户详细信息
     */
//    @PreAuthorize("@ss.hasPermi('business:completion:query')")
    @Log(title = "工艺下发及时率查询详细信息", businessType = BusinessType.SELECT)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bizProcessCompletionService.getById(id));
    }

    /**
     * 新增企业租户
     */
//    @PreAuthorize("@ss.hasPermi('business:completion:add')")
    @Log(title = "工艺下发及时率新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizProcessCompletion bizProcessCompletion) throws NoSuchFieldException, IllegalAccessException {
        setTenantId(bizProcessCompletion);
        return toAjax(bizProcessCompletionService.save(bizProcessCompletion));
    }

    /**
     * 修改企业租户
     */
//    @PreAuthorize("@ss.hasPermi('business:completion:edit')")
    @Log(title = "工艺下发及时率修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizProcessCompletion bizProcessCompletion) throws NoSuchFieldException, IllegalAccessException {
        setTenantId(bizProcessCompletion);
        return toAjax(bizProcessCompletionService.updateById(bizProcessCompletion));
    }

    /**
     * 删除企业租户
     */
//    @PreAuthorize("@ss.hasPermi('business:completion:remove')")
    @Log(title = "工艺下发及时率删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(bizProcessCompletionService.removeBatchByIds(ids));
    }
}
