package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.fire.domain.DemFireInspect;
import com.usky.fire.domain.DemFireInspectPerson;
import com.usky.fire.mapper.DemFireInspectMapper;
import com.usky.fire.service.DemFireInspectPersonService;
import com.usky.fire.service.DemFireInspectService;
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.List;
import java.util.Random;

/**
 * <p>
 * 消防检查 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-11-24
 */
@Service
public class DemFireInspectServiceImpl extends AbstractCrudService<DemFireInspectMapper, DemFireInspect> implements DemFireInspectService {

    @Autowired
    private DemFireInspectPersonService demFireInspectPersonService;

    @Override
    public CommonPage<DemFireInspect> fireInspectList(String companyId, Integer pageNum, Integer pageSize) {
        IPage<DemFireInspect> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DemFireInspect> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotBlank(companyId), DemFireInspect::getCompanyId, companyId)
                .orderByDesc(DemFireInspect::getId);
        page = this.page(page, queryWrapper);
        if (CollectionUtils.isNotEmpty(page.getRecords())) {
            List<Integer> personIdList = new ArrayList<>();
            for (int i = 0; i < page.getRecords().size(); i++) {
                personIdList.add(page.getRecords().get(i).getPersonId());
            }
            List<DemFireInspectPerson> demFireInspectPeople = demFireInspectPersonService.fireInspectPersonList(null, null, personIdList);
            for (int i = 0; i < page.getRecords().size(); i++) {
                for (int j = 0; j < demFireInspectPeople.size(); j++) {
                    if (page.getRecords().get(i).getPersonId() == demFireInspectPeople.get(j).getId()) {
                        page.getRecords().get(i).setPersonName(demFireInspectPeople.get(j).getPersonName());
                        page.getRecords().get(i).setPersonPosition(demFireInspectPeople.get(j).getPersonPosition());
                        page.getRecords().get(i).setLinkPhone(demFireInspectPeople.get(j).getLinkPhone());
                    }
                }
            }
        }
        return new CommonPage<>(page.getRecords(), page.getTotal(), pageSize, pageNum);
    }

    @Transactional
    @Override
    public DemFireInspectPerson addFireInspect(DemFireInspect demFireInspect) {
        List<DemFireInspectPerson> list = demFireInspectPersonService.fireInspectPersonList(null, null, null);
        int value = 0;
        if (list.size() != 1) {
            Random random = new Random();
            value = random.nextInt(list.size());
        }
        demFireInspect.setPersonId(list.get(value).getId());
        demFireInspect.setPlanTime(LocalDateTime.now());
        demFireInspect.setCreator(SecurityUtils.getUsername());
        demFireInspect.setCreateTime(LocalDateTime.now());
        this.save(demFireInspect);
        return list.get(value);
    }

    @Override
    public void updateFireInspect(DemFireInspect demFireInspect) {
        this.updateById(demFireInspect);
    }

}
