package com.bonus.material.purchase.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import cn.hutool.core.convert.Convert;
import com.bonus.common.biz.config.ListPagingUtil;
import com.bonus.common.core.utils.ServletUtils;
import com.bonus.common.log.enums.OperaType;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.material.common.annotation.PreventRepeatSubmit;
import com.bonus.material.purchase.domain.PurchaseCheckDetails;
import com.bonus.material.purchase.domain.PurchaseSignRecord;
import com.bonus.material.purchase.domain.dto.PurchaseCheckDto;
import com.bonus.material.purchase.domain.dto.PurchaseQueryDto;
import com.bonus.material.purchase.domain.vo.PurchaseCheckFormVo;
import com.bonus.material.purchase.domain.vo.PurchaseVerifyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.material.purchase.domain.PurchaseCheckInfo;
import com.bonus.material.purchase.service.IPurchaseCheckInfoService;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.utils.poi.ExcelUtil;
import static com.bonus.common.core.web.page.TableSupport.PAGE_NUM;
import static com.bonus.common.core.web.page.TableSupport.PAGE_SIZE;

/**
 * 新购验收任务Controller
 *
 * @author syruan
 */
@Api(tags = "新购管理接口")
@RestController
@RequestMapping("/purchase_check_info")
public class PurchaseCheckInfoController extends BaseController {

    @Resource
    private IPurchaseCheckInfoService purchaseCheckInfoService;

    /**
     * 查询新购验收任务列表
     */
    @ApiOperation("查询新购验收任务列表")
    //@RequiresPermissions("purchase:info:list")
    @GetMapping("/list")
    public AjaxResult list(PurchaseQueryDto purchaseQueryDto) {
        final List<PurchaseCheckInfo> list = purchaseCheckInfoService.selectPurchaseCheckInfoList(purchaseQueryDto);
        final Integer pageIndex = Convert.toInt(ServletUtils.getParameter(PAGE_NUM), 1);
        final Integer pageSize = Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), 10);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }


    /**
     * 获取新购验收任务详细信息
     */
    @ApiOperation("获取新购验收任务详细信息")
    //@RequiresPermissions("purchase:info:query")
    @GetMapping(value = "/getInfo")
    public AjaxResult getInfo(@NotNull(message = "请求对象不能为空") PurchaseQueryDto purchaseQueryDto) {
        return success(purchaseCheckInfoService.selectPurchaseCheckInfoById(purchaseQueryDto));
    }

    /**
     * 获取新购验收人信息
     */
    @ApiOperation("获取新购验收人信息")
    //@RequiresPermissions("purchase:info:query")
    @GetMapping(value = "/getCheckUserList")
    public AjaxResult getCheckUserList(@NotNull(message = "请求对象不能为空") PurchaseQueryDto purchaseQueryDto) {
        return success(purchaseCheckInfoService.selectPurchaseCheckUserById(purchaseQueryDto));
    }

    /**
     * 新增新购验收任务
     */
    @ApiOperation("新增新购验收任务")
    @PreventRepeatSubmit
    //@RequiresPermissions("purchase:info:add")
    @SysLog(title = "新购验收任务", businessType = OperaType.INSERT, module = "物资新购->新增新购验收任务")
    @PostMapping
    public AjaxResult purchase_check_infoadd(@Valid @NotNull @RequestBody PurchaseCheckDto purchaseCheckDto) {
        try {
            return purchaseCheckInfoService.insertPurchaseCheckInfo(purchaseCheckDto);
        } catch (final Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 修改新购验收任务
     */
    @ApiOperation("修改新购验收任务")
    @PreventRepeatSubmit
    //@RequiresPermissions("purchase:info:edit")
    @SysLog(title = "新购验收任务", businessType = OperaType.UPDATE, module = "物资新购->修改新购验收任务")
    @PutMapping
    public AjaxResult edit(@RequestBody @NotNull(message = "请求参数不能为空") @Valid PurchaseCheckDto purchaseCheckDto) {
        try {
            return toAjax(purchaseCheckInfoService.updatePurchaseCheckInfo(purchaseCheckDto));
        } catch (final Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 修改新购验收任务
     * @param purchaseCheckDto
     * @return
     */
    @ApiOperation("修改新购验收任务")
    @PostMapping("/update")
    public AjaxResult update(@RequestBody @NotNull(message = "请求参数不能为空") @Valid PurchaseCheckDto purchaseCheckDto) {
        try {
            return toAjax(purchaseCheckInfoService.update(purchaseCheckDto));
        } catch (final Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 外层批量验收API
     */
    @ApiOperation("外层批量验收API")
    @PreventRepeatSubmit
    //@RequiresPermissions("purchase:info:edit")
    @SysLog(title = "新购验收任务", businessType = OperaType.UPDATE, module = "物资新购->外层批量验收API")
    @PutMapping("/outerVerify")
    public AjaxResult outerVerify(@RequestBody PurchaseVerifyVo purchaseVerifyVo) {
        return purchaseCheckInfoService.outVerify(purchaseVerifyVo);
    }

    /**
     * 外层批量验收API
     */
    @ApiOperation("内层批量验收API")
    @PreventRepeatSubmit
    //@RequiresPermissions("purchase:info:edit")
    @SysLog(title = "新购验收任务", businessType = OperaType.UPDATE, module = "物资新购->内层批量验收API")
    @PutMapping("/innerVerify")
    public AjaxResult innerVerify(@RequestBody PurchaseVerifyVo purchaseVerifyVo) {
        return purchaseCheckInfoService.innerVerify(purchaseVerifyVo);
    }


    /**
     * 根据任务id查询验收单
     */
    @ApiOperation("根据任务id查询验收单--taskId")
    //@RequiresPermissions("purchase:info:query")
    @SysLog(title = "新购验收任务", businessType = OperaType.UPDATE, module = "物资新购->查询验收单API")
    @GetMapping("/getPurchaseCheckFormByTaskId/{taskId}")
    public AjaxResult getPurchaseCheckFormByTaskId(@PathVariable("taskId") Long taskId) {
        final PurchaseCheckFormVo purchaseCheckFormVo = purchaseCheckInfoService.selectPurchaseCheckFormVoByTaskId(taskId);
        return AjaxResult.success(purchaseCheckFormVo);
    }



    /**
     * 删除新购验收任务
     */
    @ApiOperation("删除新购验收任务")
    @PreventRepeatSubmit
    //@RequiresPermissions("purchase:info:remove")
    @SysLog(title = "新购验收任务", businessType = OperaType.DELETE, module = "物资新购->删除新购验收任务")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(purchaseCheckInfoService.deletePurchaseCheckInfoByIds(ids));
    }



    /**
     * 导出新购验收任务列表
     */
    @ApiOperation("导出新购验收任务列表")
    @PreventRepeatSubmit
    //@RequiresPermissions("purchase:info:export")
    @SysLog(title = "新购验收任务", businessType = OperaType.EXPORT, module = "物资新购->导出新购验收任务")
    @PostMapping("/export")
    public void export(HttpServletResponse response, PurchaseQueryDto purchaseQueryDto) {
        List<PurchaseCheckInfo> list = purchaseCheckInfoService.selectPurchaseCheckInfoList(purchaseQueryDto);
        ExcelUtil<PurchaseCheckInfo> util = new ExcelUtil<>(PurchaseCheckInfo.class);
        util.exportExcel(response, list, "新购验收任务数据");
    }

    /**
     * 导出新购验收任务详细列表
     */
    @ApiOperation("导出新购验收任务详细列表")
    @PreventRepeatSubmit
    //@RequiresPermissions("purchase:info:export")
    @SysLog(title = "新购验收任务详细", businessType = OperaType.EXPORT, logType = 1,module = "物资新购->导出新购验收任务详细")
    @PostMapping("/exportDetails")
    public void exportDetails(HttpServletResponse response, PurchaseQueryDto purchaseQueryDto) {
        PurchaseCheckDto dto = purchaseCheckInfoService.selectPurchaseCheckInfoById(purchaseQueryDto);
        List<PurchaseCheckDetails> list = dto.getPurchaseCheckDetailsList();
        ExcelUtil<PurchaseCheckDetails> util = new ExcelUtil<>(PurchaseCheckDetails.class);
        util.exportExcel(response, list, "新购验收任务详细数据");
    }

    /**
     * 根据任务id查询会签记录
     * @param purchaseSignRecord
     * @return
     */
    @ApiOperation("根据任务id查询会签记录")
    @GetMapping("/getSignList")
    public AjaxResult getSignList(PurchaseSignRecord purchaseSignRecord) {
        List<PurchaseSignRecord> list = purchaseCheckInfoService.getSignList(purchaseSignRecord);
        return AjaxResult.success(list);
    }
}
