package org.springblade.manager.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.manager.dto.FormulaBean;
import org.springblade.manager.dto.WbsParamBean;
import org.springblade.manager.entity.Formula;
import org.springblade.manager.entity.WbsParam;
import org.springblade.manager.service.IWbsParamService;
import org.springblade.manager.wrapper.FormulaWrapper;
import org.springblade.manager.wrapper.WbsParamWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author yangyj
 * @Date 2022/6/17 17:06
 * @description TODO
 */
@RestController
@AllArgsConstructor
@RequestMapping("/param")
@Api(value = "节点参数", tags = "节点参数")
public class WbsParamController {
    private final IWbsParamService service;

    /**
     * 保存或修改
     * @param wp
     * @return
     */
    @PostMapping("/saveOrUpdate")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "保存或修改参数", notes = "保存或修改参数")
    public R saveOrUpdate(@RequestBody WbsParamBean wp) {
        if(wp.getId()==null){
            WbsParam e= new WbsParam();
            BeanUtils.copyProperties(wp,e);
            return R.status(this.service.save(e));
        }else{
            WbsParam old = this.service.getById(wp.getId());
            if(old!=null){
                BeanUtils.copyProperties(wp,old);
                return R.status(service.updateById(old));
            }
            return R.status(false);
        }
    }

    @PostMapping("/saveOrUpdateBatch")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "批量保存或修改参数", notes = "批量保存或修改参数")
    public R saveOrUpdateBatch(@RequestBody List<WbsParamBean> wps) {
       if(CollectionUtil.isNotEmpty(wps)){
           List<Long> ids= wps.stream().map(WbsParamBean::getId).filter(Objects::nonNull).collect(Collectors.toList());
           if(CollectionUtil.isNotEmpty(ids)){
               this.service.update(Wrappers.<WbsParam>update().lambda().set(WbsParam::getIsDeleted,1).notIn(WbsParam::getId,ids));
           }
           Map<Boolean,List<WbsParamBean>> map = wps.stream().collect(Collectors.partitioningBy(e->e.getId()!=null));
           List<WbsParamBean> saveList = map.get(false);
           List<WbsParamBean> updateList = map.get(true);
           if(CollectionUtil.isNotEmpty(saveList)){
               this.service.saveBatch(BeanUtil.copy(saveList,WbsParam.class));
           }
           if(CollectionUtil.isNotEmpty(updateList)){
               List<WbsParam> ul = new ArrayList<>();
               for(WbsParamBean b:updateList){
                   WbsParam old = this.service.getById(b.getId());
                   BeanUtils.copyProperties(b,old);
                   ul.add(old);
               }
               this.service.saveOrUpdateBatch(ul);
           }
           return  R.status(true);
       }
       return R.status(false);
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "删除", notes = "传入id集合")
    public R remove(@ApiParam(value="需要删除的Id,多个用逗号逗号分开",required = true)@RequestParam String ids) {
        return R.status(this.service.deleteLogic(Func.toLongList(ids)));
    }
    /**
     * 查询单条
     */
    @ApiOperationSupport(order =3)
    @ApiOperation(value = "查看详情", notes = "传入id")
    @GetMapping("/detail")
    @ApiIgnore
    public R<WbsParamBean> detail(WbsParam wp) {
        WbsParam detail = service.getOne(Condition.getQueryWrapper(wp));
        if(detail!=null){
            return R.data(BeanUtil.copy(detail,WbsParamBean.class));
        }
        return R.success("无数据");
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "节点参数列表(翻页)", notes = "节点参数列表(翻页)")
    @GetMapping("/page")
    public IPage<WbsParamBean> list( WbsParamBean param,  Query query) {
        LambdaQueryWrapper<WbsParam> queryWrapper = Wrappers.<WbsParam>query().lambda();
        IPage<WbsParam> pages  = this.service.page(Condition.getPage(query),queryWrapper);
        return WbsParamWrapper.build().pageVO(pages);
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "获取名称字典", notes = "获取名称字典")
    @GetMapping("/keymap")
    public R<List<WbsParamBean>> list2() {
        LambdaQueryWrapper<WbsParam> queryWrapper = Wrappers.<WbsParam>query().lambda().eq(WbsParam::getType,0);
        List<WbsParamBean> list = new ArrayList<>();
        List<WbsParam> data =  this.service.list(queryWrapper);
        if(CollectionUtil.isNotEmpty(data)){
            list = WbsParamWrapper.build().listVO(this.service.list(queryWrapper));
        }
        return R.data(list);
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "节点参数列表", notes = "节点参数列表")
    @GetMapping("/list")
    public R<List<WbsParamBean>> list3(@ApiParam(value = "wbs节点id", required = true) Long wbsId) {
        LambdaQueryWrapper<WbsParam> queryWrapper = Wrappers.<WbsParam>query().lambda().eq(WbsParam::getWbsId,wbsId).eq(WbsParam::getType,1);
        List<WbsParamBean> list = new ArrayList<>();
        List<WbsParam> data =  this.service.list(queryWrapper);
        if(CollectionUtil.isNotEmpty(data)){
            list = WbsParamWrapper.build().listVO(this.service.list(queryWrapper));
        }
        return R.data(list);
    }



}
