package com.foreverwin.mesnac.anomaly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.foreverwin.mesnac.anomaly.mapper.AbnormalCauseMapper;
import com.foreverwin.mesnac.anomaly.model.AbnormalBill;
import com.foreverwin.mesnac.anomaly.model.AbnormalBillDispose;
import com.foreverwin.mesnac.anomaly.model.AbnormalCause;
import com.foreverwin.mesnac.anomaly.service.AbnormalCauseService;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.FrontPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 异常原因 服务实现类
 * </p>
 *
 * @author robert
 * @since 2021-07-01
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AbnormalCauseServiceImpl extends ServiceImpl<AbnormalCauseMapper, AbnormalCause> implements AbnormalCauseService {


    @Autowired
    private AbnormalCauseMapper abnormalCauseMapper;

    @Override
    public IPage<AbnormalCause> selectPage(FrontPage<AbnormalCause> frontPage, AbnormalCause abnormalCause) {
        QueryWrapper<AbnormalCause> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(abnormalCause);
        return super.page(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<AbnormalCause> selectList(AbnormalCause abnormalCause) {
        QueryWrapper<AbnormalCause> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(abnormalCause);
        return super.list(queryWrapper);
    }

    @Override
    public void insertByAbnormalBill(AbnormalBill abnormalBill, AbnormalBillDispose abnormalBillDispose, List<String> dutyCauseType) {

        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        LocalDateTime now = LocalDateTime.now();
        //判断原因分类是否为空,如果不为空，把它放进Z_ABNORMAL_CAUSE表里面，先删除，后保存
        ArrayList<AbnormalCause> causeTypes = new ArrayList<>();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("ABNORMAL_BILL_BO",abnormalBill.getHandle());
        hashMap.put("ABNORMAL_BILL_DISPOSE_BO",abnormalBillDispose.getHandle());
        abnormalCauseMapper.deleteByMap(hashMap);
        if(dutyCauseType != null && dutyCauseType.size() >= 1){
            for(String cause:dutyCauseType){
                AbnormalCause abnormalCause = new AbnormalCause();
                String uuid = UUID.randomUUID().toString();;
                abnormalCause.setSite(site);
                abnormalCause.setAbnormalBillBo(abnormalBill.getHandle());
                abnormalCause.setAbnormalBillDisposeBo(abnormalBillDispose.getHandle());
                // List<AbnormalCause> abnormalCauses = abnormalCauseService.selectList(abnormalCause);
//                if(abnormalCauses.size() != 0){
//                    QueryWrapper<AbnormalCause> abnormalCauseQueryWrapper = new QueryWrapper<>();
//                    abnormalCauseQueryWrapper.setEntity(abnormalCause);
//                    abnormalCauseService.remove(abnormalCauseQueryWrapper);
//                }
                abnormalCause.setHandle(uuid);
                abnormalCause.setAbnormalCauseNo(cause);
                abnormalCause.setCreatedDateTime(now);
                abnormalCause.setCreatedUser(user);
                abnormalCause.setModifiedUser(user);
                abnormalCause.setModifiedDateTime(now);
                causeTypes.add(abnormalCause);
            }
        }
        if(causeTypes.size() > 0){
            this.saveOrUpdateBatch(causeTypes);
        }
    }


}