package com.persagy.dmp.rwd.delivery.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.persagy.dmp.basic.model.QueryCriteria;
import com.persagy.dmp.basic.utils.QueryCriteriaHelper;
import com.persagy.dmp.common.constant.CommonConstant;
import com.persagy.dmp.common.constant.ResponseCode;
import com.persagy.dmp.common.context.AppContext;
import com.persagy.dmp.common.exception.BusinessException;
import com.persagy.dmp.common.model.response.CommonResult;
import com.persagy.dmp.common.utils.ResultHelper;
import com.persagy.dmp.delivery.entity.ObjectTypeCollect;
import com.persagy.dmp.rwd.delivery.service.IObjectTypeCollectService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 项目，现场交付范围管理-对象类型
 * @author:linhuili
 * @date:2021/10/13
 */
@RestController
@RequestMapping("/rwd/def/class/collect")
public class ObjectTypeCollectController {

    @Autowired
    private IObjectTypeCollectService service;


    /**
     * 查询对象类型交付范围
     * @param criteria
     * @return
     */
    @PostMapping("/query")
    public CommonResult<List<ObjectTypeCollect>> query(@RequestBody QueryCriteria criteria) {
        if(criteria == null) {
            throw new BusinessException(ResponseCode.A0400.getCode(), ResponseCode.A0400.getDesc());
        }
        QueryWrapper<ObjectTypeCollect> wrapper = new QueryWrapper<>();
        // 转换查询条件
        QueryCriteriaHelper.toWrapper(wrapper, criteria, ObjectTypeCollect.class);
        Page page = QueryCriteriaHelper.toPage(criteria);
        if(criteria.isOnlyCount()) {
            Integer total = service.queryCount(wrapper);
            page.setTotal(total);
        } else {
            page = service.queryByCondition(QueryCriteriaHelper.toPage(criteria), wrapper);
        }
        return ResultHelper.multi(page.getRecords(), page.getTotal());
    }

    /**
     * 添加对象类型交付范围
     * @param vo
     * @return
     */
    @PostMapping("/create")
    public CommonResult<ObjectTypeCollect> create(@Valid @RequestBody ObjectTypeCollect vo) throws Exception{
        List<ObjectTypeCollect> voList = CollUtil.newArrayList(vo);
        //创建默认条件
        createDefaultValue(voList);
        //新增信息点
        service.insert(voList);
        return ResultHelper.single(vo);
    }

    /**
     * 批量添加对象类型交付范围
     * @param voList
     * @return
     */
    @PostMapping("/createBatch")
    public CommonResult<List<ObjectTypeCollect>> createBatch(@Valid @RequestBody List<ObjectTypeCollect> voList) throws Exception{
        //创建默认条件
        createDefaultValue(voList);
        //批量新增信息点
        voList = service.insert(voList);
        return ResultHelper.multi(voList);
    }

    /**
     * 修改对象类型交付范围
     * @param vo
     * @return
     */
    @PostMapping("/update")
    public CommonResult<ObjectTypeCollect> update(@Valid @RequestBody ObjectTypeCollect vo) throws Exception{
        List<ObjectTypeCollect> voList = CollUtil.newArrayList(vo);
        //创建默认条件
        createDefaultValue(voList);
        //修改信息点
        service.update(voList);
        return ResultHelper.single(vo);
    }

    /**
     * 批量修改对象类型交付范围
     * @param voList
     * @return
     */
    @PostMapping("/updateBatch")
    public CommonResult<List<ObjectTypeCollect>> updateBatch(@Valid @RequestBody List<ObjectTypeCollect> voList) throws Exception{
        //创建默认条件
        createDefaultValue(voList);
        //修改信息点
        voList = service.update(voList);
        return ResultHelper.multi(voList);
    }

    /**
     * 删除对象类型交付范围
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public CommonResult delete(@RequestBody String id){
        if(StringUtils.isEmpty(id)){
            throw new BusinessException(ResponseCode.A0400.getCode(), "id不能为空");
        }
        service.delete(CollUtil.newArrayList(id));
        return ResultHelper.success();
    }

    /**
     * 批量删除对象类型交付范围
     * @param ids
     * @return
     */
    @PostMapping("/deleteBatch")
    public CommonResult deleteBatch(@RequestBody List<String> ids){
        if(ids == null || ids.size()<=0){
            throw new BusinessException(ResponseCode.A0400.getCode(), "id不能为空");
        }
        service.delete(ids);
        return ResultHelper.success();
    }

    /**
     * 创建默认条件
     * @param voList
     */
    private void createDefaultValue(List<ObjectTypeCollect> voList){
        String groupCode = AppContext.getContext().getGroupCode();
        if(StringUtils.isEmpty(groupCode)){
            //默认集团
            groupCode = CommonConstant.DEFAULT_ID;
        }
        String projectId = AppContext.getContext().getProjectId();
        if(StringUtils.isEmpty(projectId)){
            //默认项目
            projectId = CommonConstant.DEFAULT_ID;
        }
        for (ObjectTypeCollect vo : voList) {
            if(StringUtils.isEmpty(vo.getGroupCode())){
                vo.setGroupCode(groupCode);
            }
            if(StringUtils.isEmpty(vo.getProjectId())){
                vo.setProjectId(projectId);
            }
            //默认有效标识
            if(vo.getValid() == null){
                vo.setValid(1);
            }
        }
    }

}
