package cn.com.countrygarden.bi.main.controller.domainname;

import cn.com.countrygarden.bi.main.controller.BaseController;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.dto.domainname.DomainNameDTO;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.domainname.DomainCostData;
import cn.com.countrygarden.bi.main.model.domainname.DomainName;
import cn.com.countrygarden.bi.main.model.insuranceOrder.InsuranceOrder;
import cn.com.countrygarden.bi.main.service.AccessTokenService;
import cn.com.countrygarden.bi.main.service.domainname.DomainnameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 域名管理接口
 * </p>
 *
 * @author lxin
 * @version V1.0
 * @date
 */

@RestController("DomainnameController")
@RequestMapping("/api/domainname")
public class DomainnameController extends BaseController {

    @Autowired
    private AccessTokenService accessTokenService;
    @Autowired
    private DomainnameService domainnameService;
    @Autowired
    private HttpServletRequest request;

//导航、高级检索
    @GetMapping(value = "/listDomainname", produces = "application/json;charset=utf-8")
    public Result list(DomainNameDTO domainName,
                       @RequestParam(value = "size", required = false, defaultValue = "10") int size,
                       @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                       HttpServletRequest request) {
        AccessToken token = this.getAccessToken();
//        Map<String, Object> timeMap = new HashMap<String, Object>();
//        timeMap.put("beginDate", beginDate);
//        timeMap.put("endDate", endDate);
        //return domainnameService.showDomainnameList(domainName,token.getUserCode(),page,size,timeMap);
        return domainnameService.showDomainnameList(domainName, token.getUserCode(), page, size);
    }

    /**
     * 获取授权信息
     *
     * @return
     */
    public AccessToken getAccessToken() {
//      根据授权获取用户信息
        String accessToken = this.request.getParameter("AccessToken");
        AccessToken token = accessTokenService.findByToken(accessToken);
        return token;
    }

    @PostMapping(value = "/add")
    public Result add(@RequestParam(value = "domainName", required = true) DomainName domainName, @RequestBody List<DomainCostData> domainCostData,
                      HttpServletRequest request) {
        return domainnameService.add(domainName, domainCostData, this.getAccessToken().getUserCode());
    }


    /**
     * 查看单条明细
     *
     * @return
     */
    @GetMapping(value = "/one", produces = "application/json;charset=utf-8")
    public Result queryById(@RequestParam(value = "id", required = true) Integer id,
                            HttpServletRequest request) {
        return domainnameService.queryById(id);
    }

    // @PutMapping(value = "/edit")
    @PostMapping(value = "/edit")
    public Result edit(DomainName domainName, HttpServletRequest request) {
        return domainnameService.edit(domainName);
    }

    /**
     * 发表
     *
     * @return
     */
    @GetMapping(value = "/publish")
    public Result publishById(@RequestParam(value = "domain_id", required = true) Integer id,
                              HttpServletRequest request) {
        return domainnameService.publishById(id);
    }


    @GetMapping(value = "/deleteMyself")
    public Result deleteMyself(@RequestParam(value = "domain_id", required = true) Integer domain_id) {
        //String usercode = this.getAccessToken().getUserCode();
        return domainnameService.deleteMyself(domain_id);
    }

}

