package com.xzx.gc.system.controller;  
 | 
  
 | 
import cn.hutool.core.bean.BeanUtil;  
 | 
import cn.hutool.core.collection.CollUtil;  
 | 
import cn.hutool.core.convert.Convert;  
 | 
import cn.hutool.core.date.DateUtil;  
 | 
import cn.hutool.core.lang.Dict;  
 | 
import cn.hutool.core.util.NumberUtil;  
 | 
import cn.hutool.core.util.StrUtil;  
 | 
import com.github.pagehelper.PageHelper;  
 | 
import com.github.pagehelper.PageInfo;  
 | 
import com.xzx.gc.common.Result;  
 | 
import com.xzx.gc.common.annotations.PassToken;  
 | 
import com.xzx.gc.common.constant.Constants;  
 | 
import com.xzx.gc.common.constant.OrderEnum;  
 | 
import com.xzx.gc.common.dto.CommonDto;  
 | 
import com.xzx.gc.common.dto.SimplePage;  
 | 
import com.xzx.gc.common.request.BaseController;  
 | 
import com.xzx.gc.common.utils.BusinessUtil;  
 | 
import com.xzx.gc.common.utils.LocationUtils;  
 | 
import com.xzx.gc.common.utils.StringUtils;  
 | 
import com.xzx.gc.entity.ConfigInfo;  
 | 
import com.xzx.gc.entity.OrderInfo;  
 | 
import com.xzx.gc.entity.OtherUserInfo;  
 | 
import com.xzx.gc.entity.StoreInfo;  
 | 
import com.xzx.gc.model.JsonResult;  
 | 
import com.xzx.gc.model.user.AddressVo;  
 | 
import com.xzx.gc.system.dto.StoreInfoUpDto;  
 | 
import com.xzx.gc.system.mapper.*;  
 | 
import com.xzx.gc.system.service.AddressService;  
 | 
import com.xzx.gc.system.service.OtherUserService;  
 | 
import com.xzx.gc.system.service.StoreService;  
 | 
import io.swagger.annotations.Api;  
 | 
import io.swagger.annotations.ApiOperation;  
 | 
import org.springframework.beans.factory.annotation.Autowired;  
 | 
import org.springframework.validation.annotation.Validated;  
 | 
import org.springframework.web.bind.annotation.*;  
 | 
import tk.mybatis.mapper.entity.Example;  
 | 
  
 | 
import javax.servlet.http.HttpServletRequest;  
 | 
import java.math.RoundingMode;  
 | 
import java.util.ArrayList;  
 | 
import java.util.Arrays;  
 | 
import java.util.List;  
 | 
import java.util.Map;  
 | 
  
 | 
/**  
 | 
 * @author :zz  
 | 
 */  
 | 
@RestController  
 | 
@Api(tags = {"门店管理"})  
 | 
@Validated  
 | 
public class StoreInfoController extends BaseController {  
 | 
  
 | 
    @Autowired  
 | 
    private StoreInfoMapper storeInfoMapper;  
 | 
  
 | 
    @Autowired  
 | 
    private ConfigMapper configMapper;  
 | 
  
 | 
    @Autowired  
 | 
    private OtherUserMapper otherUserMapper;  
 | 
  
 | 
    @Autowired  
 | 
    private OrderMapper orderMapper;  
 | 
  
 | 
  
 | 
    @Autowired  
 | 
    private StoreService storeService;  
 | 
  
 | 
    @Autowired  
 | 
    private AddressService addressService;  
 | 
  
 | 
    @Autowired  
 | 
    private OtherUserService otherUserService;  
 | 
  
 | 
  
 | 
    @PostMapping("/storeInfo/select")  
 | 
    @ApiOperation(value = "分页查询门店", notes = "extra代表模糊查询字段")  
 | 
    public Result<PageInfo<StoreInfo>> find(HttpServletRequest request, @RequestBody SimplePage simplePage) {  
 | 
        ConfigInfo configInfo = new ConfigInfo();  
 | 
        configInfo.setConfigTypeCode("STORE_STATUS");  
 | 
        ConfigInfo configInfo1 = configMapper.selectOne(configInfo);  
 | 
        String configValue = configInfo1.getConfigValue();  
 | 
        PageHelper.startPage(simplePage.getPageNo(), simplePage.getPageSize());  
 | 
  
 | 
  
 | 
        Example example = new Example(StoreInfo.class);  
 | 
        Example.Criteria criteria = example.createCriteria();  
 | 
        criteria.andNotEqualTo("id",Constants.DEFAULT_ID);  
 | 
        if (Constants.CLOSE.equals(configValue)) {  
 | 
            criteria.andEqualTo("userId", getUserId(request));  
 | 
        }  
 | 
        criteria.andEqualTo("delFlag", 0);  
 | 
//        criteria.andEqualTo("flag", 0);  
 | 
  
 | 
        String extra = simplePage.getExtra();  
 | 
        if (StrUtil.isNotBlank(extra)) {  
 | 
            extra="%"+extra+"%";  
 | 
            example.and().orLike("mobilePhone", extra).orLike("userName", extra).orLike("addressArea", extra).orLike("detailAddress", extra);  
 | 
        }  
 | 
        example.setOrderByClause("create_time desc,flag asc");  
 | 
        List<StoreInfo> storeInfos = storeInfoMapper.selectByExample(example);  
 | 
        for (StoreInfo storeInfo : storeInfos) {  
 | 
            String provinceId = storeInfo.getProvinceId();  
 | 
            String cityId = storeInfo.getCityId();  
 | 
            String townshipId = storeInfo.getTownshipId();  
 | 
            if(StrUtil.isNotBlank(provinceId)&&StrUtil.isNotBlank(cityId)&&StrUtil.isNotBlank(townshipId)){  
 | 
                AddressVo byId = addressService.findByLevelId(provinceId, cityId, townshipId);  
 | 
                if(byId!=null){  
 | 
                    storeInfo.setProvinceName(byId.getProvinceName());  
 | 
                    storeInfo.setTownshipName(byId.getTownshipName());  
 | 
                    storeInfo.setCityName(byId.getCityName());  
 | 
                }  
 | 
            }  
 | 
        }  
 | 
        PageInfo<StoreInfo> pageInfo = new PageInfo<>(storeInfos);  
 | 
        return Result.success(pageInfo);  
 | 
    }  
 | 
  
 | 
    @PostMapping("/storeInfo/selectUser")  
 | 
    @ApiOperation(value = "查询门店回收员", notes = "目前只能查出自己,可兼容以后的固定门店")  
 | 
    public Result<List<OtherUserInfo>> selectUser(HttpServletRequest request) {  
 | 
        List<OtherUserInfo> list = new ArrayList<>();  
 | 
        ConfigInfo configInfo = new ConfigInfo();  
 | 
        configInfo.setConfigTypeCode("STORE_STATUS");  
 | 
        ConfigInfo configInfo1 = configMapper.selectOne(configInfo);  
 | 
        String configValue = configInfo1.getConfigValue();  
 | 
        if (Constants.CLOSE.equals(configValue)) {  
 | 
            OtherUserInfo otherUserInfo = otherUserMapper.selectByPrimaryKey(getUserId(request));  
 | 
            list.add(otherUserInfo);  
 | 
        } else if (Constants.OPEN.equals(configValue)) {  
 | 
            list=otherUserService.find();  
 | 
        }  
 | 
        return Result.success(list);  
 | 
    }  
 | 
  
 | 
  
 | 
    @PostMapping("/storeInfo/add")  
 | 
    @ApiOperation(value = "添加门店")  
 | 
    public Result add(HttpServletRequest request, @Validated @RequestBody StoreInfo storeInfo) {  
 | 
        AddressVo byName = addressService.findByName(storeInfo.getProvinceName(), storeInfo.getCityName(), storeInfo.getTownshipName());  
 | 
        if(byName!=null){  
 | 
            storeInfo.setProvinceId(byName.getProvinceId());  
 | 
            storeInfo.setCityId(byName.getCityId());  
 | 
            storeInfo.setTownshipId(byName.getTownshipId());  
 | 
        }  
 | 
        //真实门店关闭的状态下只能新增一个门店  
 | 
        StoreInfo storeInfo1 = new StoreInfo();  
 | 
        storeInfo1.setUserId(storeInfo.getUserId());  
 | 
        storeInfo1.setDelFlag(Constants.DEL_NOT_FLAG + "");  
 | 
        int i = storeInfoMapper.selectCount(storeInfo1);  
 | 
        if (i > 0) {  
 | 
            return Result.error(-1, "一个用户只能对应一个门店");  
 | 
        }  
 | 
        storeInfo.setCreateTime(DateUtil.now());  
 | 
        storeInfoMapper.insertSelective(storeInfo);  
 | 
        return Result.success("");  
 | 
    }  
 | 
  
 | 
  
 | 
    @PostMapping("/storeInfo/delete")  
 | 
    @ApiOperation(value = "删除门店", notes = "id:传列表返回的门店ID")  
 | 
    public Result delete(HttpServletRequest request, @Validated @RequestBody CommonDto commonDto) {  
 | 
        String id = commonDto.getId();  
 | 
        if (isOrder(id)) {  
 | 
            return Result.error(-1, "用户已经下单,您不能进行删除操作");  
 | 
        }  
 | 
        StoreInfo storeInfo = new StoreInfo();  
 | 
        storeInfo.setId(Long.parseLong(id));  
 | 
        storeInfo.setDelFlag(Constants.DEL_FLAG + "");  
 | 
        storeInfoMapper.updateByPrimaryKeySelective(storeInfo);  
 | 
        return Result.success("");  
 | 
    }  
 | 
  
 | 
    @PostMapping("/storeInfo/update")  
 | 
    @ApiOperation(value = "修改门店")  
 | 
    public Result update(HttpServletRequest request, @Validated @RequestBody StoreInfoUpDto storeInfoUpDto) {  
 | 
        if (isOrder(storeInfoUpDto.getId().toString())) {  
 | 
            return Result.error(-1, "用户已经下单,您不能进行修改操作");  
 | 
        }  
 | 
  
 | 
        StoreInfo storeInfo = new StoreInfo();  
 | 
        BeanUtil.copyProperties(storeInfoUpDto, storeInfo);  
 | 
  
 | 
        AddressVo byName = addressService.findByName(storeInfo.getProvinceName(), storeInfo.getCityName(), storeInfo.getTownshipName());  
 | 
        if(byName!=null){  
 | 
            storeInfo.setProvinceId(byName.getProvinceId());  
 | 
            storeInfo.setCityId(byName.getCityId());  
 | 
            storeInfo.setTownshipId(byName.getTownshipId());  
 | 
        }  
 | 
  
 | 
        storeInfoMapper.updateByPrimaryKeySelective(storeInfo);  
 | 
        return Result.success("");  
 | 
    }  
 | 
  
 | 
    @PostMapping("/storeInfo/selectNearby")  
 | 
    @ApiOperation(value = "查看附近门店", notes = "参数:extra传经纬度以逗号分隔,返回data为空则是无门店")  
 | 
    @PassToken  
 | 
    public Result<StoreInfo> selectNearby(HttpServletRequest request, @RequestBody CommonDto commonDto) {  
 | 
        String longitude = commonDto.getExtra().split(",")[0];  
 | 
        String latitude = commonDto.getExtra().split(",")[1];  
 | 
  
 | 
        if (StrUtil.isNotBlank(latitude) && StrUtil.isNotBlank(longitude)) {  
 | 
            List<StoreInfo> storeInfos=storeService.select(true);  
 | 
            double temp = Double.MAX_VALUE;  
 | 
            Dict dict = Dict.create();  
 | 
            if (CollUtil.isNotEmpty(storeInfos)) {  
 | 
                for (StoreInfo storeInfo : storeInfos) {  
 | 
                    if (StringUtils.isNotBlank(storeInfo.getLatitude()) && StringUtils.isNotBlank(storeInfo.getLongitude())) {  
 | 
                        double distance = LocationUtils.getDistance(Convert.toDouble(storeInfo.getLatitude()), Convert.toDouble(storeInfo.getLongitude()), Convert.toDouble(latitude), Convert.toDouble(longitude));  
 | 
                        if (distance < temp) {  
 | 
                            temp = distance;  
 | 
                            dict.set(Convert.toStr(temp), storeInfo);  
 | 
                        }  
 | 
                    }  
 | 
                }  
 | 
  
 | 
                if (NumberUtil.compare(temp, Double.MAX_VALUE) != 0) {  
 | 
                    StoreInfo storeInfo = (StoreInfo) dict.get(Convert.toStr(temp));  
 | 
                    double v = temp / 1000;  
 | 
                    String s = NumberUtil.roundStr(Convert.toStr(v), 3, RoundingMode.DOWN);  
 | 
                    storeInfo.setDistance(s);  
 | 
  
 | 
                    //价格升高的百分比  
 | 
                    ConfigInfo configInfo = new ConfigInfo();  
 | 
                    configInfo.setConfigTypeCode("STORE_PRICE");  
 | 
                    ConfigInfo configInfo1 = configMapper.selectOne(configInfo);  
 | 
                    String configValue = configInfo1.getConfigValue();  
 | 
                    storeInfo.setScale(configValue);  
 | 
                    return Result.success(storeInfo);  
 | 
                }  
 | 
            }  
 | 
        }  
 | 
  
 | 
        return Result.success(null);  
 | 
    }  
 | 
  
 | 
  
 | 
  
 | 
    @PostMapping("/admin/front/storeInfo/queryStoreList.json")  
 | 
    @ApiOperation(value="订单管理-查询所有门店", notes="test: 仅0有正确返回")  
 | 
    public JsonResult<List<Map<String,Object>>> queryStoreList(){  
 | 
        List<Map<String,Object>> list = storeInfoMapper.queryStoreList();  
 | 
        return JsonResult.success(list);  
 | 
    }  
 | 
  
 | 
  
 | 
    /**  
 | 
     * 用户是否已下单  
 | 
     *  
 | 
     * @param id 门店id  
 | 
     * @return  
 | 
     */  
 | 
    private boolean isOrder(String id) {  
 | 
        //判断是否已经有用户下单在此门店 否则不允许删除  
 | 
        Example example = new Example(OrderInfo.class);  
 | 
        Example.Criteria criteria = example.createCriteria();  
 | 
        criteria.andIn("orderStatus", Arrays.asList(OrderEnum.待接单.getValue(), OrderEnum.服务中.getValue(), OrderEnum.待确认.getValue()));  
 | 
        criteria.andEqualTo("storeId", id);  
 | 
        int i = orderMapper.selectCountByExample(example);  
 | 
        if (i > 0) return true;  
 | 
        return false;  
 | 
    }  
 | 
  
 | 
}  
 |