/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.install.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.common.service.IFileInfoService;
import com.tofly.install.dto.ApplyInstallQuery;
import com.tofly.install.entity.ApplyInstall;
import com.tofly.install.service.ApplyInstallService;
import com.tofly.stocks.dto.CheckTaskQuery;

import lombok.AllArgsConstructor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
 * 报装业务表
 *
 * @author admin
 * @date Fri Nov 25 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/applyInstall")
@Api(tags="报装业务表接口")
public class ApplyInstallController {

    private final ApplyInstallService applyInstallService;
    
    /**
    * 分页查询
    * @param page 分页对象
    * @param materialType 库存物资类型表
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    public ResultRespone getApplyinstallPage(Page page, @Validated ApplyInstallQuery applyInstallQuery) { 
    	return ResultRespone.success(applyInstallService.getDataPage(page, applyInstallQuery));    	
    }
    
    
    /**
     * 分页查询
     * @param page 分页对象
     * @param materialType 库存物资类型表
     * @return
     */
     @GetMapping("/queryPage" )
     @ApiOperation(value = "分页查询")
     public ResultRespone getQueryPage(Page page, @Validated ApplyInstallQuery applyInstallQuery) {
    	 try {
    			return ResultRespone.success(applyInstallService.getDataOtherPage(page, applyInstallQuery));
    	 }catch (Exception e) {
			e.printStackTrace();
			return ResultRespone.failed(e.getMessage());
		 }
    	    
     }


    /**
     * 通过id查询报装业务表
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/{id}" )
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone getById(@PathVariable("id" ) Long id) {
      return ResultRespone.success(applyInstallService.getById(id));
    }

    /**
     * 新增报装业务
     * @param applyInstall 报装业务
     * @return ResultRespone
     */
    @ToFlyAppLog(title="新增报装业务" )
    @ApiOperation(value = "新增报装业务")
    @PostMapping
    public ResultRespone save(ApplyInstall applyInstall,
            @RequestPart(required = false) @ApiParam("附件列表") List<MultipartFile> files) {
    	try {

    		return ResultRespone.success(applyInstallService.saveApplyInstall(applyInstall,files));
    	}catch (Exception e) {
    		return ResultRespone.failed(e.getMessage());
		}
    }

    /**
     * 修改报装业务
     * @param applyInstall 报装业务表
     * @return ResultRespone
     */
    @ToFlyAppLog(title="修改报装业务" )
    @ApiOperation(value = "修改报装业务")
    @PutMapping
    public ResultRespone updateById(@Validated ApplyInstall applyInstall,
            @RequestPart(required = false) @ApiParam("附件列表") List<MultipartFile> files) {
    	
    	try {
    		return ResultRespone.success(applyInstallService.editApplyInstall(applyInstall,files));
    	}catch (Exception e) {
    		return ResultRespone.failed(e.getMessage());
		}
    }
    
    
    /**
     * 修改报装业务数据状态
     * @param applyInstall 修改报装业务数据状态
     * @return ResultRespone
     */
    @ToFlyAppLog(title="修改报装业务数据状态" )
    @ApiOperation(value = "修改报装业务数据状态  ")
    @PostMapping("/updateApplyInstall" )
    public ResultRespone updateApplyInstall(@RequestBody ApplyInstall applyInstall) {
    	
    	try {
    		return ResultRespone.success(applyInstallService.updateById(applyInstall));
    	}catch (Exception e) {
    		return ResultRespone.failed(e.getMessage());
		}
    }
    
    
    
    

    /**
     * 通过id删除报装业务
     * @param id id
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id删除报装业务" )
    @ApiOperation(value = "通过ID删除报装业务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @DeleteMapping("/{id}" )
    public ResultRespone removeById(@PathVariable Long id) {
      return ResultRespone.success(applyInstallService.removeById(id));
    }

    /**
     * 通过id批量删除库存物资类型表
     * @param ids id
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id批量删除报装申请记录" )
    @ApiOperation(value = "通过ID批量删除报装申请记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds" )
    public ResultRespone removeById(String ids) {	
    	try {
    		List<String> delList = Arrays.asList(ids.split(","));
    		List<ApplyInstall> applyList = applyInstallService.list(Wrappers.<ApplyInstall>lambdaQuery().
    			eq(ApplyInstall::getApplyStatus,2).in(ApplyInstall::getId, delList));
    		if(applyList.size()!=0) {
    			throw new Exception("删除记录中包含已提交报装申请，请检查。");
    		}else {
    			for(String s:delList) {
    	    		ApplyInstall ai =new ApplyInstall();
    	    		ai.setId(Long.valueOf(s));
    	    		ai.setDelFlag(2);
    	    		applyInstallService.updateById(ai);
    	    	}   
    		}
            return ResultRespone.success("删除成功");
    	}catch (Exception e) {
    		return ResultRespone.failed(e.getMessage());
		}
    }        
}