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.install.InstallDetailDto;
import com.zcxk.rmcp.api.dto.install.InstallInputDto;
import com.zcxk.rmcp.api.dto.install.InstallPlanInputDto;
import com.zcxk.rmcp.api.query.InstallPlanQueryCondition;
import com.zcxk.rmcp.api.vo.DeviceStatisticsVo;
import com.zcxk.rmcp.api.vo.InstallPlanVo;
import com.zcxk.rmcp.api.vo.PlanStatisticsVo;
import com.zcxk.rmcp.core.entity.InstallList;
import com.zcxk.rmcp.web.service.install.InstallListService;
import com.zcxk.rmcp.web.service.install.InstallPlanService;
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 java.util.List;

/**
 * @author linqingwei
 * @date 2021-07-21 9:48
 */
@Controller
@ResponseBody
@RequestMapping("installPlan")
@Api(tags = "安装计划")
public class InstallPlanController {

    @Autowired
    private InstallPlanService installPlanService;


    @GetMapping("getPage")
    @ApiOperation(value = "查询安装计划列表分页")
    public AjaxMessage<Pagination<InstallPlanVo>> getPage(
            @ApiParam(value = "名称", required = false) @RequestParam(required = false) String name,
            @ApiParam(value = "公司id", required = false) @RequestParam(required = false) Integer companyOrgId,
            @ApiParam(value = "部门id", required = false) @RequestParam(required = false) Integer deptOrgId,
            @ApiParam(value = "计划状态，0:未结束，1:已结束", required = false) @RequestParam(required = false) Integer planStatus,
            @ApiParam(value = "验收状态，0:未结束，1:已结束", required = false) @RequestParam(required = false) Integer acceptStatus,
            @ApiParam(value = "页数，默认第一条", required = true, defaultValue = "1") @RequestParam(required = true, defaultValue = "1") int pageNum,
            @ApiParam(value = "每页显示数，默认第15条", required = true, defaultValue = "15") @RequestParam(required = true, defaultValue = "15") int pageSize,
            HttpServletRequest httpServletRequest
    ) {

        InstallPlanQueryCondition condition = new InstallPlanQueryCondition();
        condition.setName(name);
        condition.setCompanyOrgId(companyOrgId);
        condition.setDeptOrgId(deptOrgId);
        condition.setPlanStatus(planStatus);
        condition.setAcceptStatus(acceptStatus);


        Pagination<InstallPlanVo> list = installPlanService.getPage(condition, pageNum, pageSize);
        return  AjaxMessage.success(list);
    }


    @PostMapping("add")
    @ApiOperation(value = "添加安装计划")
    public AjaxMessage<Void> add(
            @ApiParam(value = "安装计划", required = true) @RequestBody(required = true) InstallPlanInputDto installPlanInput
    ) {
        installPlanService.add(installPlanInput);
        return AjaxMessage.success();
    }

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

    @PostMapping("allDel")
    @ApiOperation(value = "全部删除安装计划")
    public AjaxMessage<Void> allDel(
            @ApiParam(value = "名称", required = false) @RequestParam(required = false) String name,
            @ApiParam(value = "公司id", required = false) @RequestParam(required = false) Integer companyOrgId,
            @ApiParam(value = "部门id", required = false) @RequestParam(required = false) Integer deptOrgId,
            @ApiParam(value = "计划状态，0:未结束，1:已结束", required = false) @RequestParam(required = false) Integer planStatus,
            @ApiParam(value = "验收状态，0:未结束，1:已结束", required = false) @RequestParam(required = false) Integer acceptStatus
    ) {
        InstallPlanQueryCondition condition = new InstallPlanQueryCondition();
        condition.setName(name);
        condition.setCompanyOrgId(companyOrgId);
        condition.setDeptOrgId(deptOrgId);
        condition.setPlanStatus(planStatus);
        condition.setAcceptStatus(acceptStatus);

        installPlanService.allDel(condition);
        return AjaxMessage.success();
    }

    @GetMapping("getInstallDetail")
    @ApiOperation(value = "查询安装计划详情")
    public AjaxMessage<InstallDetailDto> getInstallDetail(
            @ApiParam(value = "安装计划id", required = true) @RequestParam(required = true) Integer id
    ) {
        InstallDetailDto detail = installPlanService.getInstallDetail(id);
        return AjaxMessage.success(detail);
    }



    @GetMapping("downPlanTemplate")
    @ApiOperation(value = "下载计划模板", notes = "下载计划模板")
    public void planDownTemplate(
            @ApiParam(value = "access_token", required = true) @RequestParam(required = true) String accessToken,
            HttpServletRequest request, HttpServletResponse response
    ) {
        installPlanService.downPlanTemplate(response);
    }


    @GetMapping("/statistics")
    @ApiOperation(value = "设备统计")
    public AjaxMessage<PlanStatisticsVo> statisticsDevice(
            @ApiParam(value = "安装计划id", required = true) @RequestParam(required = true) Integer id
    ) {
        return AjaxMessage.success(installPlanService.statistics(id));
    }





}
