package com.dayou.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.dayou.brokerage.annotation.DoBrokerage;
import com.dayou.brokerage.constants.BrokerageBusiness;
import com.dayou.brokerage.constants.BrokerageRule;
import com.dayou.brokerage.constants.BrokerageState;
import com.dayou.common.BaseEntity;
import com.dayou.entity.ItemBrokerageGeneral;
import com.dayou.entity.ItemEvaluate;
import com.dayou.entity.ItemStage;
import com.dayou.enums.ItemStateEnum;
import com.dayou.enums.OperationTypeEnum;
import com.dayou.exception.ErrorCode;
import com.dayou.mapper.ItemEvaluateMapper;
import com.dayou.service.IItemBrokerageGeneralService;
import com.dayou.service.IItemEvaluateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayou.service.IItemService;
import com.dayou.service.IItemStageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dayou.utils.ExcelUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.ArrayList;
import org.springframework.transaction.annotation.Transactional;
import com.dayou.enums.BatchTaskTypeEnum;

import static com.dayou.exception.ErrorCode.ITEM_EVALUATE_ERROR;

/**
 * <p>
 * 项目完结评价表 服务实现类
 * </p>
 *
 * @author wucl
 * @since 2023-03-03
 */
@Service
public class ItemEvaluateServiceImpl extends ServiceImpl<ItemEvaluateMapper, ItemEvaluate> implements IItemEvaluateService {

    @Autowired
    private IItemService iItemService;

    @Autowired
    private IItemBrokerageGeneralService iItemBrokerageGeneralService;

    @Override
    @SuppressWarnings("unchecked")
    public Page<ItemEvaluate> selectPage(Page page,ItemEvaluate itemEvaluate){
        return this.page(page, new QueryWrapper<ItemEvaluate>(itemEvaluate));
    }


    @Override
    public ItemEvaluate detail(Long id){
        return this.getById(id);
    }

    @Transactional
    @DoBrokerage(operation = OperationTypeEnum.SETTLE,rule = BrokerageRule.LAND_OTHER_RULE)
    @Override
    public Boolean add(ItemEvaluate itemEvaluate){
        String itemState = iItemService.getItemState(itemEvaluate.getItemId());
        if (!ItemStateEnum.COMPLETED.getName().equals(itemState)){
            ErrorCode.throwBusinessException(ITEM_EVALUATE_ERROR);
        }
        return  this.save(itemEvaluate);
    }

    @DoBrokerage(operation = OperationTypeEnum.SETTLE,rule = BrokerageRule.LAND_OTHER_RULE)
    @Override
    public Boolean update(ItemEvaluate itemEvaluate){
        String itemState = iItemService.getItemState(itemEvaluate.getItemId());
        if (!ItemStateEnum.COMPLETED.getName().equals(itemState)){
            ErrorCode.throwBusinessException(ITEM_EVALUATE_ERROR);
        }
        return  this.updateById(itemEvaluate);
    }

    @Override
    public Boolean delete(Long id){
        //逻辑删除
        return this.removeById(id);
    }

    @Override
    public ItemEvaluate detailByItemId(Long itemId) {
        ItemEvaluate itemEvaluate = this.getOne(new LambdaQueryWrapper<ItemEvaluate>().eq(ItemEvaluate::getItemId, itemId));
        return itemEvaluate;
    }
}
