package com.kiisoo.ic.customer.controller;

import com.kiisoo.ic.common.BaseController;
import com.kiisoo.ic.customer.CustomerService;
import com.kiisoo.ic.customer.bean.OpCustomerDTO;
import com.kiisoo.ic.customer.entity.OpCustomer;
import com.kiisoo.ic.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 客户
 */
@RestController
@RequestMapping("/customer")
@Slf4j
public class CustomerController extends BaseController {

    @Autowired
    private CustomerService customerService;

    @ResponseBody
    @PostMapping("/info")
    public Map<String, Object> getCustomerInfo(@Param("userId") long userId, Long shopId, Long regionId , Long sellerId, Long companyId,
                               String startDate, String endDate,@RequestParam("pageNum") int pageNum, @RequestParam("pageSize")int pageSize){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<OpCustomerDTO> opCustomerDTOS = null;
        try {
            opCustomerDTOS = customerService.getCustomerByUserAndDate(userId, shopId, regionId, sellerId,companyId,
                    StringUtils.isBlank(startDate) ? null : sdf.parse(startDate),  StringUtils.isBlank(endDate) ? null : DateUtils.addDay(sdf.parse(endDate),1) ,pageNum,pageSize);
            return data(opCustomerDTOS);

        } catch (ParseException e) {
            log.error("日期转换异常:",e);
            return fail();

        }
    }

    /**
     * 修改客户信息
     * @param customerId 客户id
     * @param name 客户名称
     * @return
     */
    @ResponseBody
    @PostMapping("/edit/info")
    public Map<String, Object> editCustomerInfo(@RequestParam("customerId") long customerId, String name){
        try {
            if(null!=name){
                OpCustomer opCustomer = new OpCustomer();
                opCustomer.setId(customerId);
                opCustomer.setName(name);
                customerService.editCustomerInfo(opCustomer);
            }
            return success();
        } catch (Exception e) {
            log.error("修改客户信息异常:",e);
            return  fail();
        }
    }

    /**
     * 查询好友列表
     * @param userId
     * @param sellerId
     * @param search
     * @param roleCode
     * @param type 1.会员好友 2.普通好友 (默认全部)
     * @return
     */
    @ResponseBody
    @PostMapping("/list/friends")
    public Map<String, Object> listCustomer(@RequestParam("userId") Long userId,
                                            @RequestParam(value = "sellerId",required = false)Long sellerId,
                                            @RequestParam(value = "search",required = false)String search,
                                            @RequestParam("roleCode")String roleCode,
                                            @RequestParam("type") Integer type){
        try {
            Map<String, Object> stringObjectMap = customerService.listCustomer(userId, sellerId, search, roleCode, type);
            return data(stringObjectMap);
        } catch (Exception e) {
            log.error("修改客户信息异常:",e);
            return  fail();
        }
    }
}
