java --關注/取消關注

業務需求:A、B兩個用戶的關注/取消關注接口java

1.接口文檔以下web


2.數據庫設計以下數據庫


3.數據庫數據存放以下app


由數據庫設計文檔和數據庫存放關係可知,數據庫設計

數據 1000000004 既能夠在數據庫的A位置,也能夠在數據庫的B位置。ide

3.該接口的webservice思路以下設計

關注/取消關注 用戶的思路
參數:用戶A、用戶B、關注/取消關注flag
A和B的關係,假設A在數據庫的A位置
00 A關注B
01 B關注A
02 A、B相互關注
備註:A在數據庫的B位置時,如上關係爲0一、00、02
思路以下:
0.去數據庫查詢A、B這兩個用戶有沒有關係(00/01/02)
1.點擊關注 --說明A和B的關係:1>沒有關係;2>B關注A
1-1.判斷A和B的關係
1-2.若是沒有關係,則添加一條數據
1-3.若是有關係,則將關係改成相互關注(這個要結合實際,若是A已經關注了B,則A不能在關注B了,即進來
的確定是B點擊了關注A)
2.點擊取消關注 --說明A和B的關係:1>A關注B;2>A、B相互關注
2-1.判斷A和B的關係
2-2.若是是關注(結合實際,只有相互關注和關注兩種關係),則取消關注,即刪除這條數據
2-3.若是是相互關注,則須要判斷A和B的位置
2-3-1 若是A在數據庫的A位置,則修改A和B的關係爲被關注--即關係爲01
2-3-2 若是A在數據庫的B位置,則修改A和B的關係爲被關注--即關係爲00

4.該接口的java代碼以下code

@Service
public class UserFollowServiceImpl implements UserFollowService {

    @Autowired
    private UserFollowMapper userFollowMapper;

    /**
     * 關注(取消關注)用戶
     * 
     * @param userIdB
     *            被關注者id
     * @param userId
     *            用戶Id
     * @param flag
     *            0:關注 1:取消關注
     */
    @Override
    public ResultObject updateOrInsertUserFocus(UserFollowVO userFollowVO)
            throws DataAccessException {
        boolean needBreak = false;
        String userId = userFollowVO.getUserId();
        String userIdB = userFollowVO.getUserIdB();
        String flag = userFollowVO.getFlag();
        ResultObject ro = new ResultObject();
        ro.setResultCode(ResultCode.FAILED);

        if (StringUtils.isBlank(userIdB) || StringUtils.isBlank(userId)
                || StringUtils.isBlank(flag)) {
            ro.setResultCode(ResultCode.FAILED);
            ro.setResultMsg(ResultMsg.MSG_PARAM_ERROR);
            needBreak = true;
        }
        if (!needBreak) {
            // 當前時間
            String timeNow = Utils.getSysTime();
            // 查找雙方關係
            UserFollow userFollow = userFollowMapper.updateOrInsertUserFocus(
                    userId, userIdB);
            // 點擊須要關注
            if (flag.equals(Constant.FOLLOW)) {
                if (userFollow != null) {
                    // 已經存在關注關係,改成相互關注
                    userFollowMapper.updUserFocus(userId, userIdB,
                            Constant.A_BOTH_B, timeNow);
                    int temp = userFollowMapper.selectCount(userIdB);
                    ro.setData(temp);
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("focusNums", temp);
                    ro.setData(map);
                    ro.setResultCode(ResultCode.SUCCESS);
                } else {
                    // 不存在關注關係,改成單方面關注
                    userFollowMapper.insertUserFocus(userId, userIdB,
                            Constant.A_FOLLOW_B, timeNow);
                    int temp = userFollowMapper.selectCount(userIdB);
                    ro.setData(temp);
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("focusNums", temp);
                    ro.setData(map);
                    ro.setResultCode(ResultCode.SUCCESS);
                }

                ro.setResultMsg(ResultMsg.MSG_FOLLOW_SUCCESS);
            } else {
                // 取消關注
                if (userFollow != null) {
                    // 相互關注
                    if (userFollow.getFollowFlag().equals(Constant.A_BOTH_B)) {
                        // 判斷操做用戶是否在A位置
                        int count = userFollowMapper.getUserIdPosition(userId,
                                userIdB);
                        // 若是關注者在A位置,被關注這在B位置,改成'01'
                        if (count > 0) {
                            userFollowMapper.updUserFocus(userId, userIdB,
                                    Constant.B_FOLLOW_A, timeNow);
                            int temp = userFollowMapper.selectCount(userIdB);
                            HashMap<String, Object> map = new HashMap<>();
                            map.put("focusNums", temp);
                            ro.setData(map);
                            ro.setResultCode(ResultCode.SUCCESS);
                        } else {
                            // 若是關注者在B位置,被關注這在A位置,改成'00'
                            userFollowMapper.updUserFocus(userId, userIdB,
                                    Constant.A_FOLLOW_B, timeNow);
                            int temp = userFollowMapper.selectCount(userIdB);
                            HashMap<String, Object> map = new HashMap<>();
                            map.put("focusNums", temp);
                            ro.setData(map);
                            ro.setResultCode(ResultCode.SUCCESS);
                        }
                    } else {
                        // 單方面關注,刪除關係
                        userFollowMapper.deleteUserFocus(userId, userIdB);
                        int temp = userFollowMapper.selectCount(userIdB);
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("focusNums", temp);
                        ro.setData(map);
                        ro.setResultCode(ResultCode.SUCCESS);
                    }
                    ro.setResultMsg(ResultMsg.MSG_CANCLE_FOLLOW_SUCCESS);
                }
            }
        }
        return ro;
    }
}
相關文章
相關標籤/搜索