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.AbnormalDutyMapper;
import com.foreverwin.mesnac.anomaly.model.AbnormalBill;
import com.foreverwin.mesnac.anomaly.model.AbnormalBillDispose;
import com.foreverwin.mesnac.anomaly.model.AbnormalDuty;
import com.foreverwin.mesnac.anomaly.service.AbnormalDutyService;
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 AbnormalDutyServiceImpl extends ServiceImpl<AbnormalDutyMapper, AbnormalDuty> implements AbnormalDutyService {


    @Autowired
    private AbnormalDutyMapper abnormalDutyMapper;

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

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

    @Override
    public void insertByAbnormalBill(AbnormalBill abnormalBill, AbnormalBillDispose abnormalBillDispose, List<String> dutyType) {
        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        LocalDateTime now = LocalDateTime.now();
        ArrayList<AbnormalDuty> dutyTypes = new ArrayList<>();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("ABNORMAL_BILL_BO",abnormalBill.getHandle());
        hashMap.put("ABNORMAL_BILL_DISPOSE_BO",abnormalBillDispose.getHandle());
        abnormalDutyMapper.deleteByMap(hashMap);
        //判断责任分类是否为空,如果不为空，把它放进Z_ABNORMAL_DUTY表里面
        if(dutyType != null && dutyType.size() >= 1){
            for(String duty:dutyType){
                AbnormalDuty abnormalDuty = new AbnormalDuty();
                String uuid = UUID.randomUUID().toString();
                abnormalDuty.setSite(site);
                abnormalDuty.setAbnormalBillBo(abnormalBill.getHandle());
                abnormalDuty.setAbnormalBillDisposeBo(abnormalBillDispose.getHandle());
               // List<AbnormalDuty> abnormalDuties = abnormalDutyService.selectList(abnormalDuty);
//                if(abnormalDuties.size() != 0){
//                    QueryWrapper<AbnormalDuty> abnormalDutyQueryWrapper = new QueryWrapper<>();
//                    abnormalDutyQueryWrapper.setEntity(abnormalDuty);
//                    abnormalDutyService.remove(abnormalDutyQueryWrapper);
//                }
                abnormalDuty.setHandle(uuid);
                abnormalDuty.setAbnormalDutyNo(duty);
                abnormalDuty.setModifiedUser(user);
                abnormalDuty.setModifiedDateTime(now);
                abnormalDuty.setCreatedDateTime(now);
                abnormalDuty.setCreatedUser(user);
                dutyTypes.add(abnormalDuty);
            }
        }

        if(dutyTypes.size() > 0){
            this.saveOrUpdateBatch(dutyTypes);
        }
    }


}