package com.pj.tb_purchaser_address;

import com.pj.current.satoken.StpAPPUserUtil;
import com.pj.utils.sg.AjaxJson;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * Controller: tb_purchaser_address -- 收购商个人地址管理表
 *
 * @author qzy
 */
@RestController
@RequestMapping("/app/address/")
public class PurchaserAddressAppController {

    /**
     * 底层 Service 对象
     */
    @Autowired
    TbPurchaserAddressService service;

    /**
     * 新增/编辑
     */
    @PostMapping("saveOrUpdate")
    public AjaxJson saveOrUpdate(@Validated TbPurchaserAddress address) {
        address.setUserId(StpAPPUserUtil.getAPPLoginInfo().getLoginId());
        return AjaxJson.toAjax(service.saveOrUpdate(address));
    }

    /**
     * 获取详情
     */
    @GetMapping("getById/{id}")
    public AjaxJson getById(@PathVariable Long id) {
        return AjaxJson.getSuccessData(service.getById(id));
    }

    @GetMapping("getByUserId")
    public AjaxJson getByUserId(@PathVariable Long id) {
        return AjaxJson.getSuccessData(service.getById(id));
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    public AjaxJson delete(@PathVariable Long id) {
        TbPurchaserAddress address = new TbPurchaserAddress();
        address.setId(id);
        address.setUserId(StpAPPUserUtil.getAPPLoginInfo().getLoginId());
        return AjaxJson.toAjax(service.deleteByIdAndUserId(address));
    }

    /**
     * 我的地址
     *
     * @return
     */
    @RequestMapping("getList")
    public AjaxJson getList() {
        SoMap so = SoMap.getRequestSoMap();
        so.put("userId", StpAPPUserUtil.getAPPLoginInfo().getLoginId());
        List<TbPurchaserAddress> list = service.getList(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);
    }

}
