package cn.com.ty.lift.ud.templateBase.service.impl;

import cn.com.ty.lift.ud.templateBase.controller.query.MaintenanceSetQuery;
import cn.com.ty.lift.ud.templateBase.mapper.MaintenanceSetMapper;
import cn.com.ty.lift.ud.templateBase.mapper.entity.MaintenanceOptionEntity;
import cn.com.ty.lift.ud.templateBase.mapper.entity.MaintenanceSetEntity;
import cn.com.ty.lift.ud.templateBase.mapper.entity.MaintenanceSetOptionEntity;
import cn.com.ty.lift.ud.templateBase.mapper.entity.MaintenanceSetVo;
import cn.com.ty.lift.ud.templateBase.service.IMaintenanceSetOptionService;
import cn.com.ty.lift.ud.templateBase.service.IMaintenanceSetService;
import cn.com.ty.lift.ud.userInfo.mapper.entity.UserInfoEntity;
import cn.com.ty.lift.ud.userInfo.service.IUserInfoService;
import cn.com.ty.lift.ud.utils.CurrentUserInfo;
import cn.com.xwy.boot.web.dto.RestResponse;
import cn.com.xwy.util.collection.ListUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.Collection;
import java.util.List;

/**
 * 维保套餐 -ServiceImpl
 *
 * @author Xson
 * @date 2020-04-29
 */
@Service
@Transactional
public class MaintenanceSetServiceImpl extends ServiceImpl<MaintenanceSetMapper, MaintenanceSetEntity>
        implements IMaintenanceSetService {


    @Autowired
    private MaintenanceSetMapper maintenanceSetMapper;


    @Autowired
    private IMaintenanceSetOptionService iMaintenanceSetOptionService;

    @Autowired
    private IUserInfoService iUserInfoService;

    /**
     * 批量启用或停用
     *
     * @param ids
     * @param status
     */
    @Override
    public void updateByIds(List<Long> ids, Integer status) {
        for (Long id : ids) {
            MaintenanceSetEntity que = new MaintenanceSetEntity();
            que.setId(id);
            que.setStatuz(status);
            maintenanceSetMapper.updateById(que);
        }
    }

    /**
     * 根据ID删除
     *
     * @param id
     * @return
     */
    @Override
    public void delete(long id) {
        //删除系统/企业模板表数据
        maintenanceSetMapper.deleteById(id);
        //删除关联表数据
        iMaintenanceSetOptionService.deletebySetId(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public void deleteBatch(Collection<Long> ids) {
        for (Long id : ids) {
            //删除系统/企业模板表数据
            maintenanceSetMapper.deleteById(id);
            //删除关联表数据
            iMaintenanceSetOptionService.deletebySetId(id);
        }
    }

    /**
     * 重写条件查询的方法
     *
     * @param query
     * @return
     */
    @Override
    public RestResponse selectByExample(MaintenanceSetQuery query) {
        IPage page = query.page();
        List<MaintenanceSetEntity> list = maintenanceSetMapper.selectByQuery(query.condition(), query);
        //查询关联 option 表的哪些ID
        if(ListUtil.isNotEmpty(list)){
            for(MaintenanceSetEntity setEntity : list){
                List<MaintenanceSetEntity> resultList = maintenanceSetMapper.getInfoById(setEntity.getId());
                if(ListUtil.isNotEmpty(resultList)){
                    List<Long> ids = new ArrayList<>();
                    for(MaintenanceSetEntity set : resultList){
                        if(null != set.getOpId()){
                            ids.add(set.getOpId());
                        }
                    }
                    setEntity.setIds(ids);
                }

            }
        }

        page.setRecords(list);
        int total = maintenanceSetMapper.countByQuery(query.condition());
        page.setTotal(total);
        return RestResponse.success(page, "Select successfully");
    }


    /**
     * 新增
     *
     * @param maintenanceSet
     * @return
     */
    @Override
    public RestResponse saveInfo(MaintenanceSetEntity maintenanceSet) {

        //模板主表插入数据
        //获取当前登录人
        String mobile = CurrentUserInfo.userMobile();
        if(null != mobile){
            //根据mobile获取userID
            UserInfoEntity userInfoEntity = iUserInfoService.getUserByMobile(mobile);
            if(null != userInfoEntity){
                maintenanceSet.setCreateBy(userInfoEntity.getUserId().toString());
                maintenanceSet.setCreateTime(LocalDateTime.now());
                Integer insert = maintenanceSetMapper.saveInfo(maintenanceSet);
                //获取 新增数据的主键
                Long id1 = maintenanceSet.getId();
                if (insert == 1) {
                    if(ListUtil.isNotEmpty(maintenanceSet.getIds())){
                        //关联表插入数据
                        MaintenanceSetOptionEntity msoEntity = new MaintenanceSetOptionEntity();
                        for (Long id : maintenanceSet.getIds()) {
                            msoEntity.setSetId(id1);
                            msoEntity.setOptionId(id);
                            iMaintenanceSetOptionService.save(msoEntity);
                        }
                    }
                }
                return RestResponse.success(null, "新建成功");
            }else{
                return RestResponse.fail( "当前用户不存在");
            }
        }else{
            return RestResponse.fail( "请重新登录");
        }
    }

    /**
     * 根据ID查询详情
     * @param id
     * @return
     */
    @Override
    public MaintenanceSetVo getInfoById(Long id) {
        List<MaintenanceSetEntity> infoByIdList = maintenanceSetMapper.getInfoById(id);
        MaintenanceSetVo msVo = new MaintenanceSetVo();
        if(null != infoByIdList){
            msVo.setName(infoByIdList.get(0).getName());
            msVo.setDriveType(infoByIdList.get(0).getDriveType());
            msVo.setValidTime(infoByIdList.get(0).getValidTime());
            List<MaintenanceOptionEntity>  moEntitylist = new ArrayList<>();
            for(MaintenanceSetEntity msEntity : infoByIdList){
                MaintenanceOptionEntity moEntity =new MaintenanceOptionEntity();
                moEntity.setId(msEntity.getOpId());
                moEntity.setOpt(msEntity.getOpt());
                moEntity.setRequirement(msEntity.getRequirement());
                moEntitylist.add(moEntity);
            }
            msVo.setOptionList(moEntitylist);
        }

        return msVo;
    }

    /**
     * 重写修改的方法
     * @param request
     * @return
     */
    @Override
    public RestResponse updateInfoById(MaintenanceSetEntity request) {
        //修改模板表的信息
        maintenanceSetMapper.updateById(request);
        //修改关联表的信息
        iMaintenanceSetOptionService.deletebySetId(request.getId());
        List<Long> ids = request.getIds();
        if(ListUtil.isNotEmpty(ids)){
            for(Long optionId : ids){
                MaintenanceSetOptionEntity msoEntity  = new MaintenanceSetOptionEntity();
                msoEntity.setOptionId(optionId);
                msoEntity.setSetId(request.getId());
                iMaintenanceSetOptionService.save(msoEntity);
            }
        }
        return RestResponse.success(null, "修改成功");
    }

}
