package com.neusoft.smart.pos.org.controller;

import com.neusoft.smart.pos.dto.org.request.PostShopRequest;
import com.neusoft.smart.pos.dto.org.request.PutShopRequest;
import com.neusoft.smart.pos.dto.org.request.ShopQueryRequest;
import com.neusoft.smart.pos.dto.org.response.PostShopResponse;
import com.neusoft.smart.pos.framework.bean.PaginationExtendBean;
import com.neusoft.smart.pos.framework.dto.ResponseData;
import com.neusoft.smart.pos.org.persistent.domain.Shop;
import com.neusoft.smart.pos.org.service.RemoteUserService;
import com.neusoft.smart.pos.org.service.ShopService;
import com.neusoft.smart.pos.utils.RequestHeaderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/shop")
public class ShopController {
    
    @Autowired
    ShopService shopService;
    
    @Autowired
    RemoteUserService remoteUserService;
    
    @GetMapping("/my")
    public ResponseData<List<Shop>> getMyShops(@RequestHeader("X-AUTH-USERNAME") String username) {
        return new ResponseData<>(shopService.selectShopsByUsername(username));
    }
    
    @GetMapping("/shopsByUser/{userId}")
    public ResponseData<List<Integer>> getShops(@PathVariable Integer userId) {
        return new ResponseData<>(shopService.selectShopIdByUserId(userId));
    }
    
    @GetMapping("/list")
    public ResponseData<PaginationExtendBean<PostShopResponse>> getShopList(ShopQueryRequest shopQueryRequest) {
        PaginationExtendBean<PostShopResponse> shopsResponse = new PaginationExtendBean<>();
        shopsResponse.setRows(shopService.selectShopsByCondi(shopQueryRequest));
        shopsResponse.setTotal(shopService.countShopsByCondi(shopQueryRequest));
        return new ResponseData<>(shopsResponse);
    }
    
    @GetMapping("/detail")
    public ResponseData<PostShopResponse> getShopDetail(@RequestParam(value = "shopId") int shopId) {
        return new ResponseData<>(shopService.selectShopById(shopId));
    }
    
    @PostMapping("/insert")
    public ResponseData<PostShopResponse> shopInsert(@RequestBody PostShopRequest shopRequest) {
        return new ResponseData<>(shopService.insertShop(shopRequest));
    }
    
    @PutMapping("/update")
    public ResponseData<Integer> shopUpdate(@RequestBody PutShopRequest shopRequest) {
        return new ResponseData<>(shopService.updateShop(shopRequest));
    }
    
    @DeleteMapping("/delete")
    public ResponseData<Integer> shopDelete(int shopId) {
        return new ResponseData<>(shopService.deleteShop(shopId));
    }
    
    @GetMapping("/shopsByIds")
    public ResponseData<List<PostShopResponse>> queryShopListByIds(@RequestParam(value = "ids") List<Integer> ids) {
        return new ResponseData<>(shopService.queryShopListByIds(ids));
    }
    
    @GetMapping("/shopsByOrgId")
    public ResponseData<List<PostShopResponse>> queryShopListByOrgId(
            @RequestParam(value = "orgId") Integer orgId
            , @RequestParam(value = "shopId", defaultValue = "0") Integer shopId) {
        return new ResponseData<>(shopService.queryShopListByOrgId(orgId, shopId));
    }
    
    @GetMapping("/shopsByUser")
    public ResponseData<List<PostShopResponse>> queryShopListByCurrentUser() {
        List<Integer> shopIds = RequestHeaderUtil.getStoreIds();
        return new ResponseData<>(shopService.queryShopListByIds(shopIds));
    }
    
    @PutMapping("/update/settle/{shopId}/{settleType}")
    public ResponseData<?> changeSettleType(@PathVariable Integer shopId, @PathVariable String settleType) {
        shopService.changeSettleType(shopId, settleType);
        return new ResponseData<>();
    }
    
}
