package com.jeeplus.psimanage.dishManage.service;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.psimanage.dishManage.domain.PsiDishRoom;
import com.jeeplus.psimanage.dishManage.mapper.PsiDishRoomMapper;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.utils.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 包房管理
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PsiDishRoomService extends ServiceImpl<PsiDishRoomMapper, PsiDishRoom> {

    @Resource
    private PsiDishRoomMapper mapper;

    public List<PsiDishRoom> list(PsiDishRoom info) {
        QueryWrapper<PsiDishRoom> wrapper = new QueryWrapper<>();
        wrapper.eq("a.del_flag", "0");
        if (StringUtils.isNotBlank(info.getRoomName())) {
            wrapper.like("a.room_name", info.getRoomName());
        }
        if (StringUtils.isNotBlank(info.getRoomNo())) {
            wrapper.like("a.room_no", info.getRoomNo());
        }
        if (StringUtils.isNotBlank(info.getStatus())) {
            wrapper.eq("a.status", info.getStatus());
        }
        return mapper.findList(wrapper);
    }

    public PsiDishRoom findById(String id) {
        return mapper.queryById(id);
    }

    public String saveRoom(PsiDishRoom info) {
        if (StringUtils.isBlank(info.getRoomNo())) {
            info.setRoomNo(genRoomNo());
        }
        Integer noCount = mapper.checkNoIsExist(info.getRoomNo(), info.getId());
        if (noCount != null && noCount > 0) {
            return "包房编号重复，请重新填写";
        }
        Integer nameCount = mapper.checkNameIsExist(info.getRoomName(), info.getId());
        if (nameCount != null && nameCount > 0) {
            return "包房名称重复，请重新填写";
        }
        UserDTO userDto = SpringUtil.getBean(IUserApi.class).getByToken(TokenProvider.getCurrentToken());
        if (StringUtils.isNotBlank(info.getId())) {
            info.setUpdateById(userDto.getId());
            info.setUpdateTime(new Date());
            mapper.updateById(info);
        } else {
            info.setId(UUID.randomUUID().toString().replace("-", ""));
            info.setCreateById(userDto.getId());
            info.setCreateTime(new Date());
            info.setUpdateById(userDto.getId());
            info.setUpdateTime(new Date());
            info.setDelFlag(0);
            if (StringUtils.isBlank(info.getStatus())) {
                info.setStatus("0");
            }
            mapper.insert(info);
        }
        return "操作完成";
    }

    public String deleteByIds(String ids) {
        if (StringUtils.isBlank(ids)) {
            return "请选择要删除的数据";
        }
        mapper.deleteBatchIds(Arrays.asList(ids.split(",")));
        return "删除成功";
    }

    public ResponseEntity<String> changeStatus(String id, String status) {
        Integer noCount = mapper.checkUseStatusIsExist(id);
        if (noCount != null && noCount > 0) {
            return ResponseEntity.badRequest().body("包房正在使用中，无法停用");
        }
        PsiDishRoom info = new PsiDishRoom();
        info.setId(id);
        info.setStatus(status);
        mapper.updateById(info);
        return ResponseEntity.ok("操作成功");
    }

    private String genRoomNo() {
        return "BF" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    }
}
