package com.gree.mall.manager.controller.engin;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.engin.ProjectRepairBean;
import com.gree.mall.manager.bean.engin.ProjectRepairVO;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.engin.ProjectRepairLogic;
import com.gree.mall.manager.plus.entity.RpProjectRepairProduct;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

@Slf4j
@RestController
@Api(value = "工程维保API", tags = {"工程维保API"})
@Validated
@RequestMapping(value = "/engin/project/repair", produces = "application/json; charset=utf-8")
public class ProjectRepairController {

    @Resource
    ProjectRepairLogic projectRepairLogic;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "工程维保-列表")
    public ResponseHelper<Page<ProjectRepairVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<ProjectRepairVO> vo = projectRepairLogic.list(zfireParamBean);
        return ResponseHelper.success(vo, new TypeReference<ProjectRepairVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation(value = "工程维保-导出")
    public void listExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<ProjectRepairVO> page = projectRepairLogic.list(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "工程维保-详情")
    public ResponseHelper<ProjectRepairBean> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) throws RemoteServiceException {
        ProjectRepairBean bean = projectRepairLogic.detail(id);
        return ResponseHelper.success(bean);
    }

    @PostMapping("/save")
    @ApiOperation(value = "工程维保-保存")
    public ResponseHelper save(
           @Valid @RequestBody ProjectRepairBean bean
    ) throws Exception {

        projectRepairLogic.save(bean);

        return ResponseHelper.success();
    }

    @PostMapping("/brand")
    @ApiOperation("工程维保-品牌(已去重)")
    public ResponseHelper<List<RpProjectRepairProduct>> brand(
            @ApiParam(value = "维保id",required = true) @RequestParam String repairProjectId
    ){
        List<RpProjectRepairProduct> brands = projectRepairLogic.brand(repairProjectId);
        return ResponseHelper.success(brands);
    }

    @PostMapping("/main")
    @ApiOperation("工程维保-大类(已去重)")
    public ResponseHelper<List<RpProjectRepairProduct>> main(
            @ApiParam(value = "维保id",required = true) @RequestParam String repairProjectId,
            @ApiParam(value = "品牌id",required = false) @RequestParam String brandId
    ){
        List<RpProjectRepairProduct> mains = projectRepairLogic.main(repairProjectId, brandId);
        return ResponseHelper.success(mains);
    }

    @PostMapping("/small")
    @ApiOperation("工程维保-小类(已去重)")
    public ResponseHelper<List<RpProjectRepairProduct>> small(
            @ApiParam(value = "维保id",required = true) @RequestParam String repairProjectId,
            @ApiParam(value = "品牌id",required = false) @RequestParam(required = false) String brandId,
            @ApiParam(value = "大类id",required = false) @RequestParam(required = false) String mainId
    ){
        List<RpProjectRepairProduct> small = projectRepairLogic.small(repairProjectId, brandId, mainId);
        return ResponseHelper.success(small);
    }


//
//    @PostMapping("/product/save")
//    @ApiOperation(value = "工程维保-设备编辑")
//    public ResponseHelper productSave(
//            @RequestBody RpProjectRepairProduct product
//    ) throws Exception {
//
//        projectRepairLogic.productSave(product);
//
//        return ResponseHelper.success();
//    }
//
//    @PostMapping("/product/del")
//    @ApiOperation(value = "工程维保-设备删除")
//    public ResponseHelper productDel(
//            @ApiParam(value = "工程维保id", required = true) @RequestParam String projectRepairId,
//            @ApiParam(value = "明细id", required = true) @RequestParam String id
//    ) throws Exception {
//
//        projectRepairLogic.productDel(projectRepairId, id);
//
//        return ResponseHelper.success();
//    }
//
//    @PostMapping("/by/save")
//    @ApiOperation(value = "工程维保-人员编辑")
//    public ResponseHelper bySave(
//            @RequestBody RpProjectRepairBy product
//    ) throws Exception {
//
//        projectRepairLogic.bySave(product);
//
//        return ResponseHelper.success();
//    }
//
//    @PostMapping("/by/del")
//    @ApiOperation(value = "工程维保-人员删除")
//    public ResponseHelper byDel(
//            @ApiParam(value = "工程维保id", required = true) @RequestParam String projectRepairId,
//            @ApiParam(value = "明细id", required = true) @RequestParam String id
//    ) throws Exception {
//
//        projectRepairLogic.byDel(projectRepairId, id);
//
//        return ResponseHelper.success();
//    }
//
//    @PostMapping("/websit/save")
//    @ApiOperation(value = "工程维保-网点编辑")
//    public ResponseHelper websitSave(
//            @RequestBody RpProjectRepairWebsit websit
//    ) throws Exception {
//
//        projectRepairLogic.websitSave(websit);
//
//        return ResponseHelper.success();
//    }
//
//    @PostMapping("/websit/del")
//    @ApiOperation(value = "工程维保-网点删除")
//    public ResponseHelper websitDel(
//            @ApiParam(value = "工程维保id", required = true) @RequestParam String projectRepairId,
//            @ApiParam(value = "明细id", required = true) @RequestParam String id
//    ) throws Exception {
//
//        projectRepairLogic.websitDel(projectRepairId, id);
//
//        return ResponseHelper.success();
//    }

}
