Commit a249f4c4 by xunj

概率随机、用户牛牌数量查询

1 parent b9ffaba9
package com.polysoft.activity.controller;
import com.polysoft.activity.model.param.UserInfoParam;
import com.polysoft.activity.service.CattleBrandService;
import com.polysoft.activity.utils.ApiResult;
import io.swagger.annotations.Api;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author lvyp
* @date 2021/1/29
*/
@RestController
@RequestMapping("/cattle")
@Api(value = "牛牌", description = "牛牌接口")
public class CattleController {
@Resource
private CattleBrandService cattleBrandService;
@GetMapping("/queryCattleBrandByUserId" )
@ResponseBody
public ApiResult queryCattleBrandByUserId(@RequestParam(value = "userId") Long userId) {
if(userId == null){
return ApiResult.error("用户不能为空");
}
return cattleBrandService.queryCattleBrandByUserId(userId);
}
}
......@@ -2,7 +2,9 @@ package com.polysoft.activity.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.polysoft.activity.model.dto.CattleBrandDto;
import com.polysoft.activity.model.vo.CattleBrandFroUserVO;
import com.polysoft.activity.model.vo.CattleBrandVO;
import org.apache.ibatis.annotations.Param;
import org.mapstruct.Mapper;
import java.util.List;
......@@ -10,4 +12,5 @@ import java.util.List;
@Mapper
public interface CattleBrandMapper extends BaseMapper<CattleBrandDto> {
List<CattleBrandVO> queryCattleBrandList();
CattleBrandFroUserVO queryCattleBrandByUserId(@Param("userId")Long userId);
}
package com.polysoft.activity.model.vo;
import lombok.Data;
/**
* @author lvyp
* @date 2021/1/29
*/
@Data
public class CattleBrandFroUserVO {
private String jiangKang;
private String fuYun;
private String zhaoCai;
private String pingAn;
private String ruYi;
}
package com.polysoft.activity.service;
import com.polysoft.activity.model.vo.CattleBrandVO;
import com.polysoft.activity.utils.ApiResult;
import java.util.List;
public interface CattleBrandService {
ApiResult queryCattleBrandList();
ApiResult queryCattleBrandByUserId(Long userId);
}
......@@ -16,4 +16,9 @@ public class CattleBrandServiceImpl implements CattleBrandService {
public ApiResult queryCattleBrandList() {
return ApiResult.ok(cattleBrandMapper.queryCattleBrandList());
}
@Override
public ApiResult queryCattleBrandByUserId(Long userId) {
return ApiResult.ok(cattleBrandMapper.queryCattleBrandByUserId(userId));
}
}
package com.polysoft.activity.utils;
/**
* @author lvyp
* @date 2021/1/29
*/
public class MathRandom {
public MathRandom(double rate0, double rate1, double rate2, double rate3, double rate4) {
this.rate0 = rate0;
this.rate1 = rate1;
this.rate2 = rate2;
this.rate3 = rate3;
this.rate4 = rate4;
}
/**
* 0出现的概率为%50
*/
public double rate0;
/**
* 1出现的概率为%20
*/
public double rate1;
/**
* 2出现的概率为%15
*/
public double rate2 ;
/**
* 3出现的概率为%10
*/
public double rate3 ;
/**
* 4出现的概率为%4
*/
public double rate4 ;
/**
* Math.random()产生一个double型的随机数,判断一下
* 例如0出现的概率为%50,则介于0到0.50中间的返回0
* @return int
*
*/
private int PercentageRandom()
{
double randomNumber;
randomNumber = Math.random();
if (randomNumber >= 0 && randomNumber <= rate0)
{
return 0;
}
else if (randomNumber >= rate0 / 100 && randomNumber <= rate0 + rate1)
{
return 1;
}
else if (randomNumber >= rate0 + rate1
&& randomNumber <= rate0 + rate1 + rate2)
{
return 2;
}
else if (randomNumber >= rate0 + rate1 + rate2
&& randomNumber <= rate0 + rate1 + rate2 + rate3)
{
return 3;
}
else if (randomNumber >= rate0 + rate1 + rate2 + rate3
&& randomNumber <= rate0 + rate1 + rate2 + rate3 + rate4)
{
return 4;
}
else if (randomNumber >= rate0 + rate1 + rate2 + rate3 + rate4
&& randomNumber <= rate0 + rate1 + rate2 + rate3 + rate4
)
{
return 5;
}
return -1;
}
/**
* 测试主程序
* @param agrs
*/
public static void main(String[] agrs)
{
int i = 0;
MathRandom a = new MathRandom(0.50,0.30,0.15,0.04,0.01);
for (i = 0; i <= 100; i++)//打印100个测试概率的准确性
{
System.out.println(a.PercentageRandom()+1);
}
}
}
......@@ -8,6 +8,18 @@
brand_type,
brand_name
from t_cattle_brand
</select>
<select id="queryCattleBrandByUserId" resultType="com.polysoft.activity.model.vo.CattleBrandFroUserVO">
SELECT
count( t.cattle_brand_id = 1 OR NULL ) as jiangKang,
count( t.cattle_brand_id = 2 OR NULL ) as fuYun,
count( t.cattle_brand_id = 3 OR NULL ) as zhaoCai,
count( t.cattle_brand_id = 4 OR NULL ) as pingAn,
count( t.cattle_brand_id = 5 OR NULL ) as ruYi
FROM
t_user_cattle t
WHERE
t.user_id =#{userId} GROUP BY t.user_id
</select>
</mapper>
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!