package com.jeeplus.pubmodules.luckyDraw.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.pubmodules.luckyDraw.domain.LuckyDrawMembers;
import com.jeeplus.pubmodules.luckyDraw.mapper.LuckyDrawMembersMapper;
import com.jeeplus.pubmodules.luckyDraw.service.dto.LuckyDrawMembersDto;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.UUID;

/**
 * @author: 王强
 * @create: 2023-02-28 11:03
 **/
@Service
@Transactional
public class LuckyDrawMembersService extends ServiceImpl<LuckyDrawMembersMapper, LuckyDrawMembers> {

    @Resource
    private LuckyDrawMembersMapper membersMapper;

    public Integer findMobilePhoneOnly(String phone) throws Exception {
        // 删除基础信息表
        Integer integer = membersMapper.selectCount(new QueryWrapper<LuckyDrawMembers>().eq("phone", phone));
        return integer;
    }

    public String remove(String id) {
        membersMapper.deleteById(id);
        return "操作成功";
    }

    public ResponseEntity deleteByIds(String ids) {
        String idArray[] = ids.split(",");
        ArrayList<String> strings = Lists.newArrayList(idArray);
        if (CollectionUtil.isNotEmpty(strings)) {
            // 删除活动
            this.removeByIds(strings);
            // 根据活动id删除奖项数据
            membersMapper.delete(new QueryWrapper<LuckyDrawMembers>().lambda().in(LuckyDrawMembers::getId, strings));
        }

        return ResponseEntity.ok("删除成功");
    }

    public String save(LuckyDrawMembersDto dto) throws Exception {
        // 获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean(IUserApi.class).getById("1");
        if (StringUtils.isNotEmpty(dto.getId())) {
            return update(dto, userDTO);
        } else {
            return add(dto, userDTO);
        }
    }

    public String add(LuckyDrawMembersDto dto, UserDTO userDTO) throws Exception {
        // 生成id
        String id = UUID.randomUUID().toString().replace("-", "");
        LuckyDrawMembers members = new LuckyDrawMembers();
        BeanUtils.copyProperties(dto, members);
        members.setId(id);
        members.setCreateById(userDTO.getId());
        members.setCreateTime(new Date());
        members.setUpdateById(userDTO.getId());
        members.setUpdateTime(new Date());
        members.setDelFlag(0);
        //插入前先看库中是否有对应的用户名称
        Integer drawMembersCount = membersMapper.getByPhone(dto);
        if (drawMembersCount == 0) {
            membersMapper.insert(members);
        } else {
            return "1";
        }
        return id;
    }

    public String update(LuckyDrawMembersDto dto, UserDTO userDTO) throws Exception {
        // 修改基础信息
        LuckyDrawMembers members = new LuckyDrawMembers();
        BeanUtils.copyProperties(dto, members);
        members.setUpdateById(userDTO.getId());
        members.setUpdateTime(new Date());
        //插入前先看库中是否有对应的用户名称
        Integer drawMembersCount = membersMapper.getByPhone(dto);
        if (drawMembersCount == 0) {
            membersMapper.updateById(members);
        } else {
            return "1";
        }

        return dto.getId();
    }

    public LuckyDrawMembersDto findById(String id) {
        LuckyDrawMembersDto dto = membersMapper.findById(id);
//        LuckyDrawMembers members = membersMapper.selectById(id);
//        BeanUtils.copyProperties(members,dto);
        return dto;
    }

    /**
     * 列表查询
     */
    public IPage<LuckyDrawMembersDto> list(Page<LuckyDrawMembersDto> page, LuckyDrawMembersDto dto) throws Exception {
        QueryWrapper<LuckyDrawMembersDto> queryWrapper = QueryWrapperGenerator.buildQueryCondition(dto, LuckyDrawMembersDto.class);

        queryWrapper.eq("a.del_flag", "0");
        if (StringUtils.isNotEmpty(dto.getEventId())) {
            queryWrapper.like("a.event_id", dto.getEventId());
        }
        if (StringUtils.isNotEmpty(dto.getName())) {
            queryWrapper.like("a.name", dto.getName());
        }
        if (StringUtils.isNotEmpty(dto.getPhone())) {
            queryWrapper.like("a.phone", dto.getPhone());
        }
        if (StringUtils.isNotEmpty(dto.getOfficeName())) {
            queryWrapper.eq("a.office_name", dto.getOfficeName());
        }
        if (StringUtils.isNotEmpty(dto.getCompanyName())) {
            queryWrapper.eq("a.company_name", dto.getCompanyName());
        }
        if (StringUtils.isNotEmpty(dto.getEventName())) {
            queryWrapper.like("b.name", dto.getEventName());
        }

        return membersMapper.findList(page, queryWrapper);
    }
}
