package com.huimv.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huimv.admin.common.token.TokenSign;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.common.utils.ResultCode;
import com.huimv.admin.common.utils.UploadImage;
import com.huimv.admin.entity.BillLuggage;
import com.huimv.admin.mapper.BillLuggageMapper;
import com.huimv.admin.service.IBillLuggageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2023-07-24
 */
@Service
public class BillLuggageServiceImpl extends ServiceImpl<BillLuggageMapper, BillLuggage> implements IBillLuggageService {

    @Autowired
    private UploadImage uploadImage;
    @Resource
    private BillLuggageMapper billLuggageMapper;

    @Override
    public Result addLuggage(HttpServletRequest httpServletRequest, String userName, String phone, String luggageDate, String luggageLocation, String luggageLocationId,Integer luggageNum, MultipartFile img,MultipartFile img2,MultipartFile img3,MultipartFile img4,MultipartFile img5) throws IOException {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        BillLuggage billLuggage = new BillLuggage();
        billLuggage.setUserName(userName);
        billLuggage.setUserId(TokenSign.getMemberIdByJwtToken(httpServletRequest));
        billLuggage.setPhone(phone);
        billLuggage.setLuggageDate(LocalDateTime.parse(luggageDate,dateTimeFormatter));
        billLuggage.setLuggageNum(luggageNum);
        billLuggage.setLuggageLocation(luggageLocation);
        billLuggage.setLuggageLocationId(Integer.parseInt(luggageLocationId));
        String content ="";
        content = getUrl(content,img);
        content = getUrl(content,img2);
        content = getUrl(content,img3);
        content = getUrl(content,img4);
        content = getUrl(content,img5);
        billLuggage.setArticlePicUrl(content);
        billLuggageMapper.insert(billLuggage);
        return Result.SUCCESS();
    }

    @Override
    public Result listPersonalLuggage(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String pageNo = paramsMap.get("pageNum");
        String pageSize = paramsMap.get("pageSize");
        Page<BillLuggage> page = new Page<>(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
        Page<BillLuggage> billLuggagePage = billLuggageMapper.selectPage(page, new QueryWrapper<BillLuggage>().eq("user_id", TokenSign.getMemberIdByJwtToken(httpServletRequest)).orderByDesc("luggage_date"));
        return new Result(ResultCode.SUCCESS,billLuggagePage,billLuggagePage.getSize());
    }

    public String getUrl(String content, MultipartFile image) throws IOException {
        if (ObjectUtil.isNotEmpty(image)){
            if ( !"".equals(content)){
                content = content +","+uploadImage.getImageCom(image);
            }else {
                content = content +uploadImage.getImageCom(image);
            }
        }
        return content;
    }
}
