package com.bonus.sgzb.material.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataUnit;
import com.bonus.sgzb.base.api.domain.DirectApplyDetails;
import com.bonus.sgzb.base.api.domain.DirectApplyInfo;
import com.bonus.sgzb.base.api.domain.LeaseOutDetails;
import com.bonus.sgzb.base.api.domain.SltAgreementInfo;
import com.bonus.sgzb.common.core.utils.StringUtils;
import com.bonus.sgzb.common.core.web.controller.BaseController;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.common.core.web.page.TableDataInfo;
import com.bonus.sgzb.common.log.enums.BusinessType;
import com.bonus.sgzb.common.security.utils.SecurityUtils;
import com.bonus.sgzb.material.domain.*;
import com.bonus.sgzb.material.service.ApplyInfoService;
import com.bonus.sgzb.material.service.TaskService;
import com.bonus.sgzb.material.service.WorkSiteDirectManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @Author：梁超
 * @date：2024/3/4 - 14:45
 * 工地直转管理
 */
@Api(tags = " 工地直转管理")
@RestController
@RequestMapping("/workSite")
public class WorkSiteDirectManageController extends BaseController {

    @Resource
    private WorkSiteDirectManageService workSiteDirectManageService;

    @Resource
    private TaskService tmTaskService;

    @Resource
    private ApplyInfoService applyInfoService;


    /**
     * 列表展示
     */
    @ApiOperation(value = "列表展示")
    @GetMapping("/list")
    public TableDataInfo list(DirectApplyInfo directApplyInfo) {
        startPage();
        List<DirectApplyInfo> directApplyInfos = workSiteDirectManageService.getList(directApplyInfo);
        return getDataTable(directApplyInfos);
    }

    /**
     * 根据协议Id查询在用数据
     */
    @ApiOperation(value = "根据协议Id查询在用数据")
    @GetMapping("/getUseringData")
    public TableDataInfo getUseringData(SltAgreementInfo sltAgreementInfo) {
        startPage();
        List<SltAgreementInfo> useringData = workSiteDirectManageService.getUseringData(sltAgreementInfo);
        return getDataTable(useringData);
    }

    /**
     * 存储退料领料数据
     */
    @ApiOperation(value = "保存直转方和接收方数据")
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody DirectApplyInfoDetails directApplyInfoDetails) {
        int id = 0;
        if (directApplyInfoDetails != null) {
            if (directApplyInfoDetails.getDirectApplyInfo() != null) {
                DirectApplyInfo directApplyInfos = directApplyInfoDetails.getDirectApplyInfo();
                directApplyInfos.setCode(workSiteDirectManageService.genderLeaseCode());
                directApplyInfos.setCreateTime(new Date());
                int i = workSiteDirectManageService.saveDirectApplyInfo(directApplyInfos);
                if (i < 0) {
                    return AjaxResult.error("保存失败");
                }
                id = directApplyInfos.getId();
            } else {
                return AjaxResult.error("保存失败");
            }

            if (directApplyInfoDetails.getDirectApplyInfoDetails() != null) {
                List<DirectApplyDetails> directApplyDetails = directApplyInfoDetails.getDirectApplyInfoDetails();
                for (DirectApplyDetails directApplyDetail : directApplyDetails) {
                    directApplyDetail.setDirectId(id);
                    int i = workSiteDirectManageService.saveDirectApplyDetails(directApplyDetail);
                    if (i < 0) {
                        return AjaxResult.error("保存失败");
                    }
                }
            }
        } else {
            return AjaxResult.error("保存失败");
        }
        return AjaxResult.success("保存成功");
    }

    @ApiOperation(value = "通过")
    @PostMapping("/pass")
    public AjaxResult pass(@RequestBody DirectPassApplyInfoDetails directApplyInfoDetails) {
        int rows = workSiteDirectManageService.passDirectApplyInfoDetails(directApplyInfoDetails);
        if (rows == 1) {
            return AjaxResult.success("已通过");
        } else {
            return AjaxResult.error("未通过");
        }
    }


    /**
     * 不通过
     */
    @ApiOperation(value = "不通过")
    @PostMapping("/refuse")
    public AjaxResult refuse(@RequestBody DirectApplyInfo directApplyInfo) {
        if (directApplyInfo != null) {
            directApplyInfo.setStatus("2");
            directApplyInfo.setAuditor(SecurityUtils.getLoginUser().getUsername());
            directApplyInfo.setAuditTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            workSiteDirectManageService.refuseDirectApplyInfo(directApplyInfo);
        } else {
            return AjaxResult.error("参数为空，审核失败");
        }
        return AjaxResult.success();
    }

    @ApiOperation(value = "查看详情")
    @GetMapping("/getInfo")
    public AjaxResult getInfo(SltAgreementInfo sltAgreementInfo) {
        return AjaxResult.success(workSiteDirectManageService.getInfoById(sltAgreementInfo));
    }
}

