package com.sqx.modules.artificer.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.artificer.dao.ArtificerDao;
import com.sqx.modules.artificer.dao.ArtificerMassageDao;
import com.sqx.modules.artificer.entity.Artificer;
import com.sqx.modules.artificer.entity.ArtificerMassage;
import com.sqx.modules.artificer.entity.MassageType;
import com.sqx.modules.artificer.service.ArtificerMassageService;
import com.sqx.modules.artificer.service.ArtificerService;
import com.sqx.modules.artificer.service.MassageTypeService;
import com.sqx.modules.material.dao.MaterialArtificerMapper;
import com.sqx.modules.material.dao.MaterialMassageMapper;
import com.sqx.modules.material.entity.MaterialArtificer;
import com.sqx.modules.material.entity.MaterialMassage;
import com.sqx.modules.material.service.MaterialArtificerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Service
public class ArtificerMassageServiceImpl extends ServiceImpl<ArtificerMassageDao, ArtificerMassage> implements ArtificerMassageService {

    @Autowired
    private ArtificerMassageDao artificerMassageDao;
    @Autowired
    private ArtificerService artificerService;
    @Autowired
    private MassageTypeService massageTypeService;
    @Autowired
    private MaterialArtificerService materialArtificerService;
    @Autowired
    private MaterialMassageMapper materialMassageMapper;
    @Override
    public Result insertArtificerMassage(String artificerMassages, Long userId) {
        String[] artificerMassageList = artificerMassages.split(",");
        Artificer artificer = artificerService.selectArtificerByUserId(userId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());
        for(int i = 0;i<artificerMassageList.length;i++){
            Integer integer = baseMapper.selectCount(new QueryWrapper<ArtificerMassage>().eq("massage_type_id", Long.parseLong(artificerMassageList[i])).eq("artificer_id", artificer.getArtificerId()));
            List<MaterialMassage> materialMassages = materialMassageMapper.selectList(new QueryWrapper<MaterialMassage>().eq("massage_type_id", Long.parseLong(artificerMassageList[i])));
            for(MaterialMassage materialMassage : materialMassages){
                MaterialArtificer materialArtificer = new MaterialArtificer();
                materialArtificer.setCreateUser(Long.valueOf("1"));
                materialArtificer.setCreateTime(DateUtil.date());
                materialArtificer.setMaterialId(materialMassage.getMaterialId());
                materialArtificer.setArtificerId(Math.toIntExact(artificer.getArtificerId()));
                materialArtificerService.insertMaterialArtificer(materialArtificer);
            }
            if(integer>0){
                MassageType massageType = massageTypeService.getById(Long.parseLong(artificerMassageList[i]));
                return Result.error(massageType.getTitle()+"，已经添加过了！");
            }
        }
        for(int i = 0;i<artificerMassageList.length;i++){
            ArtificerMassage artificerMassage=new ArtificerMassage();
            artificerMassage.setMassageTypeId(Long.parseLong(artificerMassageList[i]));
            artificerMassage.setArtificerId(artificer.getArtificerId());
            artificerMassage.setCreateTime(format);
            artificerMassage.setStatus(0);
            baseMapper.insert(artificerMassage);
        }
        return Result.success();
    }

    @Override
    public Result updateArtificerMassage(ArtificerMassage artificerMassage) {
        baseMapper.updateById(artificerMassage);
        return Result.success();
    }

    @Override
    public Result deleteArtificerMassage(Long id) {
        baseMapper.deleteById(id);
        return Result.success();
    }

    @Override
    public Result selectArtificerMassage(Long userId, Integer page, Integer limit, Integer status) {
        Page<ArtificerMassage> pages=new Page<>(page,limit);
        PageUtils pageUtils = new PageUtils(artificerMassageDao.selectArtificerMassage(pages, userId, status));
        return Result.success().put("data", pageUtils);
    }

    @Override
    public int updateArtificerMassageStatus(ArtificerMassage artificerMassage){
        return baseMapper.updateArtificerMassageStatus(artificerMassage);
    }

}