package com.bonus.material.ma.controller;

import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.material.ma.domain.DirectApplyDetails;
import com.bonus.material.ma.domain.DirectApplyInfo;
import com.bonus.material.ma.domain.DirectApplyInfoDetails;
import com.bonus.material.ma.domain.DirectPassApplyInfoDetails;
import com.bonus.material.ma.service.WorkSiteDirectManageService;
import com.bonus.material.settlement.domain.SltAgreementInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

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


/**
 * @author ：梁超
 * 工地直转管理
 */
@Api(tags = " 工地直转管理")
@RestController
@RequestMapping("/workSite")
public class WorkSiteDirectManageController extends BaseController {

    @Resource(name = "workSiteDirectManageService")
    private WorkSiteDirectManageService workSiteDirectManageService;

    /**
     * 列表展示
     */
    @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(DateUtils.getNowDate());
            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));
    }
}

