package com.bonus.material.ma.controller;

import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.core.utils.poi.ExcelUtil;
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.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.material.common.annotation.PreventRepeatSubmit;
import com.bonus.material.ma.domain.*;
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 javax.servlet.http.HttpServletResponse;
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);
    }

    /**
     * 导出机具类型历史库存列表
     */
    @ApiOperation(value = "导出工地直转列表")
    @PreventRepeatSubmit
    @SysLog(title = "导出工地直转列表", businessType = OperaType.EXPORT, logType = 1,module = "导出工地直转列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, DirectApplyInfo directApplyInfo)
    {
        List<DirectApplyInfo> list = workSiteDirectManageService.getList(directApplyInfo);
        ExcelUtil<DirectApplyInfo> util = new ExcelUtil<DirectApplyInfo>(DirectApplyInfo.class);
        util.exportExcel(response, list, "工地直转数据");
    }

    /**
     * 根据协议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().getSysUser().getNickName());
            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));
    }
}

