package com.casic.missiles.controller.business;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.casic.missiles.core.base.controller.ExportController;
import com.casic.missiles.core.page.PageFactory;
import com.casic.missiles.core.page.PageInfoBT;
import com.casic.missiles.dto.IdsDTO;
import com.casic.missiles.dto.ReturnDTO;
import com.casic.missiles.dto.ReturnUtil;
import com.casic.missiles.dto.business.outsource.OutsourcerAbilityListRequest;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.model.business.BusinessOutsourcerAbility;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.service.business.IBusinessOutsourcerAbilityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 分包方-能力表 前端控制器
 * </p>
 *
 * @author wangpeng
 * @since 2023-03-25
 */
@Api(tags = "分包管理接口-分包方业务能力")
@Slf4j
@RestController
@RequestMapping("/business/outsourcerAbility")
public class BusinessOutsourcerAbilityController extends ExportController {
    @Autowired
    private IBusinessOutsourcerAbilityService abilityService;

    @ApiOperation("分包方业务能力列表（分页）")
    @PostMapping("/listPage")
    @ResponseBody
    public ReturnDTO<PageInfoBT<BusinessOutsourcerAbility>> abilityListPage(@RequestBody @Valid OutsourcerAbilityListRequest request, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        Page<BusinessOutsourcerAbility> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(abilityService.listPage(page, request)));
    }

    @ApiOperation("分包方业务能力列表")
    @PostMapping("/list")
    @ResponseBody
    public ReturnDTO<List<BusinessOutsourcerAbility>> abilityList(@RequestBody @Valid OutsourcerAbilityListRequest request, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return ReturnUtil.success(abilityService.list(request));
    }

    @ApiOperation("业务能力新增")
    @PostMapping("/add")
    @ResponseBody
    public ReturnDTO add(@RequestBody List<BusinessOutsourcerAbility> abilities) {
        if(!CollectionUtils.isEmpty(abilities) && abilityService.saveBatch(abilities)) {
            return ReturnUtil.success();
        }
        throw new BusinessException(BusinessExceptionEnum.HANDLE_FAILED);
    }

    @ApiOperation("业务能力删除")
    @PostMapping("/delete")
    @ResponseBody
    public ReturnDTO delete(@RequestBody @Valid IdsDTO idsDTO) {
        Assert.isFalse(CollectionUtils.isEmpty(idsDTO.getIds()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        if(abilityService.removeByIds(idsDTO.getIds())) {
            return ReturnUtil.success();
        }
        throw new BusinessException(BusinessExceptionEnum.HANDLE_FAILED);
    }
}
