package com.huimv.receive.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.receive.common.token.TokenSign;
import com.huimv.receive.common.utils.*;
import com.huimv.receive.entity.BillLuggage;
import com.huimv.receive.entity.vo.LuggageVo;
import com.huimv.receive.mapper.BillLuggageMapper;
import com.huimv.receive.service.IBillLuggageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * <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 farmId, 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.setFarmId(farmId);
        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");
        String farmId = paramsMap.get("farmId");
        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)).eq("farm_id",farmId).orderByDesc("luggage_date"));
        return new Result(ResultCode.SUCCESS,billLuggagePage,billLuggagePage.getSize());
    }

    @Override
    public Result listLuggage(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String type = paramsMap.get("type");
        if ("".equals(type) || null == type) {
            type = "3";
        }
        List<LuggageVo> luggageVos = null;
        QueryWrapper<BillLuggage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId);
        if ("3".equals(type)) {
            Date timesMonthmorning = DataUill.getTimesMonthmorning();
            queryWrapper.ge("luggage_date", timesMonthmorning);
        } else if ("2".equals(type)) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 7);
            queryWrapper.ge("luggage_date", calendar.getTime());
        } else if ("1".equals(type)) {
            Date timesmorning = DataUill.getTimesmorning();
            queryWrapper.ge("luggage_date", timesmorning);
        }
        luggageVos = billLuggageMapper.list(queryWrapper);
        return new Result(ResultCode.SUCCESS, luggageVos);
    }

    @Override
    public Result selectLuggage(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String word = paramsMap.get("word");
        String startTime = paramsMap.get("startTime");
        String endTime = paramsMap.get("endTime");
        String pageNum = paramsMap.get("pageNum");
        String pageSize = paramsMap.get("pageSize");
        if ("".equals(pageNum) || null == pageNum) {
            pageNum = "1";
        }
        if ("".equals(pageSize) || null == pageSize) {
            pageSize = "10";
        }
        QueryWrapper<BillLuggage> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(startTime)) {
            startTime = startTime + " 00:00:00";
            endTime = endTime + " 23:59:59";
            queryWrapper.between("luggage_date", startTime, endTime);
        }
        queryWrapper.lambda().orderByDesc(BillLuggage::getId);
        queryWrapper.eq("farm_id", farmId);
        if (StringUtils.isNotBlank(word)) {
            queryWrapper.and(i -> i.like( "phone", word)
                    .or().like("user_name", word));
        }
        Page<BillLuggage> page = new Page<>(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        return new Result(ResultCode.SUCCESS, billLuggageMapper.selectPage(page, queryWrapper));
    }

    @Override
    public void printLuggage(HttpServletResponse httpServletRequest, Map<String, String> paramsMap) throws Exception {
        String ids = paramsMap.get("ids");
        String[] split = ids.split(",");
        List<BillLuggage> list = new ArrayList<>();
        for (String s : split) {
            BillLuggage admission = billLuggageMapper.selectById(s);
            list.add(admission);
        }
        Print.printLuggage(list);
    }

    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;
    }
}
