package com.bonus.sgzb.app.controller;

import com.bonus.sgzb.app.domain.TmTask;
import com.bonus.sgzb.app.service.BackReceiveService;
import com.bonus.sgzb.base.api.domain.BackApplyInfo;
import com.bonus.sgzb.common.core.text.Convert;
import com.bonus.sgzb.common.core.utils.ListPagingUtil;
import com.bonus.sgzb.common.core.utils.ServletUtils;
import com.bonus.sgzb.common.core.utils.poi.ExcelUtil;
import com.bonus.sgzb.common.core.web.controller.BaseController;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.common.log.annotation.Log;
import com.bonus.sgzb.common.log.enums.BusinessType;
import com.bonus.sgzb.common.security.annotation.PreventRepeatSubmit;
import com.bonus.sgzb.common.security.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

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

import static com.bonus.sgzb.common.core.constant.Constants.PAGE_NUM;
import static com.bonus.sgzb.common.core.web.page.TableSupport.PAGE_SIZE;

/**
 * 退料接收-app
 *
 * @author bns_han
 */
@RestController
@RequestMapping("/base/backReceive")
public class BackReceiveController extends BaseController {

    @Resource
    private BackReceiveService backReceiveService;

    /**
     * 退料接收列表
     *
     * @param record 查询条件
     * @return AjaxResult对象
     */
    @Log(title = "退料接收列表", businessType = BusinessType.QUERY)
    @GetMapping("getbackReceiveList")
    @RequiresPermissions("return:receive:list")
    public AjaxResult getbackReceiveList(BackApplyInfo record) {
        try {
            if (record.getFlag() == 0) {
                List<BackApplyInfo> list = backReceiveService.getbackReceiveList(record);
                return success(list);
            } else {
                startPage();
                List<BackApplyInfo> list = backReceiveService.getbackReceiveList(record);
                return success(getDataTable(list));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 导出
     *
     * @param record 查询条件
     * @return
     */
    @PostMapping("export")
//    @RequiresPermissions("return:receive:export")
    public void export(HttpServletResponse response, BackApplyInfo record) {
        List<BackApplyInfo> list = backReceiveService.getbackReceiveList(record);
        ExcelUtil<BackApplyInfo> util = new ExcelUtil<BackApplyInfo>(BackApplyInfo.class);
        util.exportExcel(response, list, "领料接收数据");
    }


    /**
     * 退料接收明细
     *
     * @param record 查询条件
     * @return AjaxResult对象
     */
    @Log(title = "退料接收明细-app", businessType = BusinessType.QUERY)
    @PostMapping("receiveView")
    public AjaxResult receiveView(@RequestBody BackApplyInfo record) {
        try {
            List<BackApplyInfo> list = backReceiveService.receiveView(record);
            return success(list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 退料接收明细
     *
     * @param record 查询条件
     * @return AjaxResult对象
     */
    @Log(title = "退料接收明细-web", businessType = BusinessType.QUERY)
    @PostMapping("receiveViewWeb")
    @RequiresPermissions("return:receive:view")
    public AjaxResult receiveViewWeb(@RequestBody BackApplyInfo record) {
        try {
            startPage();
            List<BackApplyInfo> list = backReceiveService.receiveView(record);
            return success(getDataTable(list));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 数量退料--管理方式为1的
     *
     * @param record 查询条件
     * @return AjaxResult对象
     */
    @Log(title = "退料接收-数量退料", businessType = BusinessType.INSERT)
    @PostMapping("setNumBack")
    public AjaxResult setNumBack(@RequestBody BackApplyInfo record) {
        try {
            int res = backReceiveService.setNumBack(record);
            if (res > 0) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error("退料失败，请填写数量");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 编码退料--管理方式为0的
     *
     * @param record 查询条件
     * @return AjaxResult对象
     */
    @Log(title = "退料接收-编码退料", businessType = BusinessType.INSERT)
    @PostMapping("setCodeBack")
    public AjaxResult setCodeBack(@RequestBody BackApplyInfo record) {
        try {
            int res = backReceiveService.setCodeBack(record);
            if (res > 0) {
                return AjaxResult.success("接收成功");
            } else if (res == -1) {
                return AjaxResult.error("该编码已接收");
            } else if (res == -2) {
                return AjaxResult.error("已超出退料数量！！");
            } else {
                return AjaxResult.error("接收失败");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 退料接收撤回
     *
     * @return AjaxResult对象
     */
    @Log(title = "退料接收撤回")
    @PostMapping("revoke")
    public AjaxResult revoke(@RequestBody BackApplyInfo record) {
        int res = backReceiveService.revoke(record);
        if (res > 0) {
            return AjaxResult.success("撤回成功");
        } else {
            return AjaxResult.error("撤回失败");
        }
    }

    /**
     * 退料接收-rfid退料
     *
     * @param record 查询条件
     * @return AjaxResult对象
     */
    @Log(title = "退料接收-rfid退料", businessType = BusinessType.INSERT)
    @PostMapping("setRfidCodeBack")
    public AjaxResult setRfidCodeBack(@RequestBody BackApplyInfo record) {
        try {
            int res = backReceiveService.setRfidCodeBack(record);
            if (res > 0) {
                return AjaxResult.success("接收成功");
            } else if (res == -1) {
                return AjaxResult.error("该RFID编码已接收");
            } else {
                return AjaxResult.error("接收失败");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //@Log(title = "退料接收-结束任务", businessType = BusinessType.MATERIAL)
    @PreventRepeatSubmit
    @PostMapping("endBack")
    public AjaxResult endBack(@RequestBody BackApplyInfo record) {
        try {
            int res = backReceiveService.endBack(record);
            if (res > 0) {
                return AjaxResult.success("接收成功");
            } else {
                return AjaxResult.error("未接收到数据");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Log(title = "退料接收-编号查询", businessType = BusinessType.INSERT)
    @PostMapping("codeQuery")
    public AjaxResult codeQuery(@RequestBody BackApplyInfo record) {
        try {
            List<BackApplyInfo> list = backReceiveService.codeQuery(record);
            return AjaxResult.success(list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Log(title = "退料接收-二维码查询", businessType = BusinessType.INSERT)
    @PostMapping("qrcodeQuery")
    public AjaxResult qrcodeQuery(@RequestBody BackApplyInfo record) {
        try {
            List<BackApplyInfo> list = backReceiveService.qrcodeQuery(record);
            return AjaxResult.success(list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Log(title = "退料接收-rfid编码查询", businessType = BusinessType.INSERT)
    @PostMapping("rfidCodeQuery")
    public AjaxResult rfidCodeQuery(@RequestBody BackApplyInfo record) {
        try {
            List<BackApplyInfo> list = backReceiveService.rfidCodeQuery(record);
            return AjaxResult.success(list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Log(title = "编码接收机具查询-web", businessType = BusinessType.INSERT)
    @GetMapping("getBackMachine")
    public AjaxResult getBackMachine(TmTask task) {
        try {
            startPage();
            List<TmTask> list = backReceiveService.getBackMachine(task);
            return AjaxResult.success(getDataTable(list));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Log(title = "编码接收机具查询-App", businessType = BusinessType.INSERT)
    @GetMapping("getBackMachineApp")
    public AjaxResult getBackMachineApp(TmTask task) {
        try {
            List<TmTask> list = backReceiveService.getBackMachine(task);
            return AjaxResult.success(list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Log(title = "查询退料接收记录-web", businessType = BusinessType.INSERT)
    @GetMapping("getRecord")
    public AjaxResult getRecord(TmTask task) {
        try {
            List<TmTask> list = backReceiveService.getRecord(task);
            if (list.size() == 0) {
                return AjaxResult.error("您还未退料数据");
            }
            return AjaxResult.success();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Log(title = "退料接收记录-app", businessType = BusinessType.INSERT)
    @PostMapping("backReceiveRecord")
    public AjaxResult backReceiveRecord(@RequestBody BackApplyInfo record) {
        try {
            List<BackApplyInfo> list = backReceiveService.backReceiveRecord(record);
            return AjaxResult.success(list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Log(title = "退料接收记录-web", businessType = BusinessType.INSERT)
    @PostMapping("backReceiveRecordWeb")
    public AjaxResult backReceiveRecordWeb(@RequestBody BackApplyInfo record) {
        try {
            List<BackApplyInfo> list = backReceiveService.backReceiveRecordWeb(record);
            Integer pageIndex = Convert.toInt(ServletUtils.getParameter(PAGE_NUM), 1);
            Integer pageSize = Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), 10);
            return AjaxResult.success(ListPagingUtil.paging(pageIndex,pageSize, list));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Log(title = "退料接收记录（编码接收）-web", businessType = BusinessType.INSERT)
    @PostMapping("backReceiveRecordWebPt")
    public AjaxResult backReceiveRecordWebPt(@RequestBody BackApplyInfo record) {
        try {
            List<BackApplyInfo> list = backReceiveService.backReceiveRecordWebPt(record);
            Integer pageIndex = Convert.toInt(ServletUtils.getParameter(PAGE_NUM), 1);
            Integer pageSize = Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), 10);
            return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

//    @Log(title = "退料接收记录（编码接收）-web", businessType = BusinessType.INSERT)
//    @GetMapping("backReceiveRecordWebPt")
//    public AjaxResult backReceiveRecordWebPt(@RequestBody BackApplyInfo record) {
//        startPage();
//        List<BackApplyInfo> list = backReceiveService.backReceiveRecordWebPt(record);
//        return AjaxResult.success(getDataTable(list));
//    }
}
