package com.zcxk.rmcp.web.controller;

import com.zcxk.core.common.pojo.AjaxMessage;
import com.zcxk.core.mysql.pageing.Pagination;
import com.zcxk.rmcp.api.dto.BasePageDto;
import com.zcxk.rmcp.api.dto.install.InstallQueryDto;
import com.zcxk.rmcp.api.dto.install.InstallDeviceDto;
import com.zcxk.rmcp.api.dto.install.InstallInputDto;
import com.zcxk.rmcp.web.service.install.impl.InstallListServiceImpl;
import com.zcxk.rmcp.web.service.install.impl.InstallPlanServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * @author linqingwei
 * @date 2021-07-28 16:31
 */
@Controller
@ResponseBody
@RequestMapping("install")
@Api(tags = "安装水表")
public class InstallController {
    @Autowired
    private InstallListServiceImpl installListService;
    @Autowired
    private InstallPlanServiceImpl installPlanService;

    @GetMapping("getInstallPage")
    @ApiOperation(value = "查询安装计划清单分页")
    public AjaxMessage<Pagination<InstallDeviceDto>> getInstallPage(
            @Valid InstallQueryDto dto, BasePageDto page
    ) {
        Pagination<InstallDeviceDto> pagination = installListService.getInstallPage(dto,page);
        return AjaxMessage.success(pagination);
    }

    @GetMapping("getUnInstallList")
    @ApiOperation(value = "查询未安装列表")
    public AjaxMessage<List<InstallDeviceDto>> getUnInstallList(
            @ApiParam(value = "安装计划id", required = true) @RequestParam(required = true) Integer planId,
            @ApiParam(value = "id 编辑时必传", required = false) @RequestParam(required = false) Integer id
    ) {
        List<InstallDeviceDto> list = installListService.getUnInstallList(planId,id);
        return AjaxMessage.success(list);
    }

    @PostMapping("save")
    @ApiOperation(value = "新装水表保存")
    public AjaxMessage<Void> installSave(@ApiParam(value = "装表", required = true) @RequestBody(required = true) InstallInputDto dto){
        installListService.installSave(dto);
        return AjaxMessage.success();
    }

    @PostMapping("edit")
    @ApiOperation(value = "新装水表编辑")
    public AjaxMessage<Void> installEdit(@ApiParam(value = "装表", required = true) @RequestBody(required = true)  InstallInputDto dto){
        installListService.installEdit(dto);
        return AjaxMessage.success();
    }

    @GetMapping("downInstallMeterTemplate")
    @ApiOperation(value = "下载装表模板", notes = "下载装表模板")
    public void downInstallMeterTemplate(
            @ApiParam(value = "安装计划id", required = true) @RequestParam(required = true) Integer planId,
            @ApiParam(value = "access_token", required = true) @RequestParam(required = true) String accessToken,
            HttpServletRequest request, HttpServletResponse response
    ) {
        installPlanService.downInstallMeterTemplate(planId, response);
    }

    @PostMapping("batchAccept")
    @ApiOperation(value = "批量验收")
    public AjaxMessage<Void> batchAccept(
            @ApiParam(value = "安装id", required = true) @RequestParam(required = true) List<Integer> ids
            ){
        installListService.batchAccept(ids);
        return AjaxMessage.success();
    }

    @PostMapping("allAccept")
    @ApiOperation(value = "全部验收")
    public AjaxMessage<Void> allAccept(
            @Valid InstallQueryDto dto
    ){
        installListService.allAccept(dto);
        return AjaxMessage.success();
    }

    @PostMapping("batchDelete")
    @ApiOperation(value = "批量删除")
    public AjaxMessage<Void> batchDelete(
            @ApiParam(value = "安装id", required = true) @RequestParam(required = true) List<Integer> ids
    ){
        installListService.batchDelete(ids);
        return AjaxMessage.success();
    }

    @PostMapping("allDelete")
    @ApiOperation(value = "全部删除")
    public AjaxMessage<Void> allDelete(
            @Valid InstallQueryDto dto
    ){
        installListService.allDelete(dto);
        return AjaxMessage.success();
    }


}
