package com.base.biz.controller;

import com.base.biz.mapper.TargetDemandMapper;
import com.base.biz.model.PointDTO;
import com.base.biz.model.dto.QueryBatchDeleteDTO;
import com.base.biz.model.dto.QueryImportDemandDTO;
import com.base.biz.model.dto.request.QueryTargetRelationAddDTO;
import com.base.biz.model.dto.request.QueryTargetRelationListDTO;
import com.base.biz.model.dto.request.QueryTargetRelationPointAddDTO;
import com.base.biz.model.dto.request.QueryTargetRelationUpdateDTO;
import com.base.biz.model.dto.response.QueryBatchUpdateRelationDTO;
import com.base.biz.model.dto.response.TargetDemandDTO;
import com.base.biz.service.TargetDemandService;
import com.base.orbit.web.Code;
import com.base.orbit.web.Result;
import com.github.liaochong.myexcel.core.DefaultExcelReader;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "需求管理")
@RestController
@RequestMapping("/api/v1/demand/manager")
public class DemandManagerController {

    @Resource
    TargetDemandService relationTargetService;

    @Resource
    TargetDemandMapper targetDemandMapper;

    @ApiOperation(value = "目标关系列表添加")
    @PostMapping("/relation/add")
    public Result<Boolean> addTargetRelationList(@RequestBody List<QueryTargetRelationAddDTO> request) {
        Boolean res = relationTargetService.addList(request);
        if (res) {
            return Result.success(true);
        } else {
            return Result.error(Code.ERROR_PARAM_ERROR, "输入数据为空");
        }
    }

    @ApiOperation(value = "目标关系列表修改")
    @PostMapping("/relation/update")
    public Result updateTargetRelation(@RequestBody QueryTargetRelationUpdateDTO request) {
        relationTargetService.update(request);
        return Result.success();
    }

    @ApiOperation(value = "根据parentId修改需求")
    @PostMapping("/relation/batch/update")
    public Result updateTargetRelationByParentId(@RequestBody QueryBatchUpdateRelationDTO request) {
        relationTargetService.updateByParentId(request);
        return Result.success();
    }

    @ApiOperation(value = "根据Ids删除列表")
    @PostMapping("/relation/delete")
    public Result delete(String ids) {
        relationTargetService.deleteByIds(ids);
        return Result.success();
    }

    @ApiOperation(value = "根据parentId删除列表")
    @PostMapping("/relation/delete/parent/id")
    public Result deleteByParentId(@RequestBody QueryBatchDeleteDTO request) {
        relationTargetService.deleteByParentId(request.getParentId());
        return Result.success();
    }

    @ApiOperation(value = "目标关系列表查詢")
    @GetMapping("/relation/list")
    public Result<List<TargetDemandDTO>> getTargetRelationByParentId(QueryTargetRelationListDTO request) {
        return Result.success(relationTargetService.getTargetRelationDTOList(request));
    }

    @ApiOperation(value = "目标关系parentId查詢")
    @GetMapping("/relation/parentId")
    public Result<String> getParentId() {
        return Result.success(relationTargetService.getParentId());
    }

    @ApiOperation(value = "新增关系，目标，点集合")
    @PostMapping("/relation/target/point/add")
    public Result addTargetRelationPoint(@RequestBody QueryTargetRelationPointAddDTO request) {
        List<QueryTargetRelationPointAddDTO> list = new ArrayList<>();
        list.add(request);
        relationTargetService.addTargetRelationPoint(list);
        return Result.success();
    }

    @ApiOperation(value = "需求导入")
    @PostMapping("/import")
    public Result<String> importData(@RequestParam(value = "file", required = false) MultipartFile file, String parentId) throws IOException {
        DefaultExcelReader<QueryImportDemandDTO> filter = DefaultExcelReader.of(QueryImportDemandDTO.class)
                .sheet(0)
                .rowFilter(row -> row.getRowNum() > 0);
        List<QueryImportDemandDTO> list = filter
                .read(file.getInputStream());
        List<QueryTargetRelationPointAddDTO> req = new ArrayList<>();


        String parentIdOld = targetDemandMapper.getParentIdByParentId(parentId+"_E");

        Integer index = parentIdOld==null?1:Integer.parseInt(parentIdOld.split("_")[3].replace("E",""))+1;

        for (QueryImportDemandDTO queryImportDemandDTO : list) {
            QueryTargetRelationPointAddDTO queryTargetRelationPointAddDTO = new QueryTargetRelationPointAddDTO();
            List<PointDTO> pointDTOList = new ArrayList<>();
            PointDTO pointDTO = new PointDTO();
            pointDTO.setSequence(0);
            pointDTO.setLat(queryImportDemandDTO.getLat());
            pointDTO.setLng(queryImportDemandDTO.getLng());
            pointDTOList.add(pointDTO);
            queryTargetRelationPointAddDTO.setPointDTOList(pointDTOList);
            queryTargetRelationPointAddDTO.setTargetName(queryImportDemandDTO.getTargetName());
            queryTargetRelationPointAddDTO.setType(0);
            queryTargetRelationPointAddDTO.setTargetLevel(0);
            queryTargetRelationPointAddDTO.setCountryName("未知");
            queryTargetRelationPointAddDTO.setPointDirection(0);
            queryTargetRelationPointAddDTO.setPriority(queryImportDemandDTO.getPriority());
            queryTargetRelationPointAddDTO.setParentId(parentId + "_E" + String.format("%04d", index));
            queryTargetRelationPointAddDTO.setRate(1);
            queryTargetRelationPointAddDTO.setStartTime(queryImportDemandDTO.getStartTime());
            queryTargetRelationPointAddDTO.setEndTime(queryImportDemandDTO.getEndTime());
            req.add(queryTargetRelationPointAddDTO);
            ++index;
        }
        if (StringUtils.isEmpty(parentId) || req.size() == 0) {
            return Result.error(202);
        }
        relationTargetService.addTargetRelationPoint(req);
        return Result.success();
    }

}
