package com.huimv.eartag2.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huimv.eartag2.admin.entity.BaseFarm;
import com.huimv.eartag2.admin.mapper.BaseFarmMapper;
import com.huimv.eartag2.admin.mapper.SysAccountMultilevelMapper;
import com.huimv.eartag2.admin.service.BaseFarmService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.eartag2.admin.utils.IDUtil;
import com.huimv.eartag2.admin.utils.ResultUtil;
import com.huimv.eartag2.common.utils.Result;
import com.huimv.eartag2.common.utils.ResultCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zn
 * @since 2022-11-09
 */
@Service
public class BaseFarmServiceImpl extends ServiceImpl<BaseFarmMapper, BaseFarm> implements BaseFarmService {
    @Resource
    private BaseFarmMapper baseFarmMapper;

    @Autowired
    private ResultUtil resultUtil;

    @Autowired
    private IDUtil idUtil;

    @Autowired
    private SysAccountMultilevelMapper accountMultilevelMapper;

    @Override
    public Result add(Map<String, String> maps) {
        String farmName = maps.get("farmName");
        String leader = maps.get("leader");
        String phone = maps.get("phone");
        String remark = maps.get("remark");
        String areaCode = maps.get("areaCode");
        String ip = maps.get("ip");
        String port = maps.get("port");
        String location = maps.get("location");
        String code1 = maps.get("code");
        String farmCode = createFarmCode(areaCode);

        BaseFarm baseFarm = new BaseFarm();
        baseFarm.setAreaCode(areaCode);
        baseFarm.setCode1(code1);
        baseFarm.setFarmCode(farmCode);
        baseFarm.setFarmName(farmName);
        baseFarm.setIp(ip);
        baseFarm.setLeader(leader);
        baseFarm.setRemark(remark);
        baseFarm.setPhone(phone);
        baseFarm.setPort(port);
        baseFarm.setLocation(location);
        return  resultUtil.result(baseFarmMapper.insert(baseFarm));
    }
    /**
     * @description: 生成农场编码
     * @author: 静静
     * @date: 2022/7/27 1:28 下午
     * @param: areaCode
     **/
    private String createFarmCode(String areaCode) {
        //先查这个区域有没有牧场、如果有、取最大编码顺序+1 、没有则从001直接开始
        QueryWrapper<BaseFarm> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("area_code",areaCode);
        List<BaseFarm> basePigpens = baseFarmMapper.selectList(queryWrapper);
        if (null == basePigpens || basePigpens.size()== 0){
            String substring1 = areaCode.substring(areaCode.length() - 6);
            return substring1+"001";
        }
        //当前区域中最大的牧场编码
        String farmCode = basePigpens.stream().max(Comparator.comparing(BaseFarm::getFarmCode)).get().getFarmCode();
        String substring = farmCode.substring(farmCode.length() - 3);
        int code =  Integer.parseInt(substring)+1;
        String formatCode = String.format("%03d", code);
        String substring1 = areaCode.substring(areaCode.length() - 6);
        return substring1+formatCode;
    }

    @Override
    public Result remove(String ids) {
        List<Integer> idList = idUtil.convert(ids);
        return resultUtil.result(baseFarmMapper.deleteBatchIds(idList));
    }

    @Override
    public Result update(Map<String, String> maps) {
        String id = maps.get("id");
        String farmName = maps.get("farmName");
        String leader = maps.get("leader");
        String phone = maps.get("phone");
        String remark = maps.get("remark");
        String areaCode = maps.get("areaCode");
        String ip = maps.get("ip");
        String port = maps.get("port");
        String location = maps.get("location");
        String code1 = maps.get("code");


        BaseFarm baseFarm = baseFarmMapper.selectById(id);
        baseFarm.setAreaCode(areaCode);
        baseFarm.setCode1(code1);
        baseFarm.setFarmName(farmName);
        baseFarm.setIp(ip);
        baseFarm.setLeader(leader);
        baseFarm.setRemark(remark);
        baseFarm.setPhone(phone);
        baseFarm.setPort(port);
        baseFarm.setLocation(location);
        return resultUtil.result(baseFarmMapper.updateById(baseFarm));
    }

    @Override
    public Result list(Map<String, String> paramsMap) {
        String farmName = paramsMap.get("farmName");
        String farmCode = paramsMap.get("farmCode");
        String code1 = paramsMap.get("code");
        String pageNo = paramsMap.get("pageNo");
        String pageSize = paramsMap.get("pageSize");
        String areaCode = paramsMap.get("areaCode");
        String leader = paramsMap.get("leader");
        Page<BaseFarm> page = new Page<>(Integer.valueOf(pageNo), Integer.valueOf(pageSize));
        QueryWrapper<BaseFarm> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(farmName), "farm_name", farmName);
        queryWrapper.like(StringUtils.isNotBlank(farmCode), "farm_code", farmCode);
        queryWrapper.like(StringUtils.isNotBlank(areaCode), "area_code", areaCode);
        queryWrapper.like(StringUtils.isNotBlank(leader), "leader", leader);
        Page<BaseFarm> bizBaseStagePage = baseFarmMapper.selectPage(page, queryWrapper);
        return new Result(ResultCode.SUCCESS, bizBaseStagePage);
    }
}
